A few things figuring out helped to solve this problem. for n+2 gon there are maximum n*(n+1)/2 -1 (summation of n natural numbers -1 = S) diagonals and minimum S-(n-1) diagonals, that means for S-(n-1) number of diagonals we need minimum n+2 polygon. Example : there is no diagonal for triangle , 2 diagonals for Quadrilaterals
Category: Algorithm
644
#include <iostream> #include <cstdio> #include <cstring> #include <string> using namespace std; struct node{ bool end; node *nxt[2]; node() { end=false; for(int i=0;i<2;i++) nxt[i]=NULL; } }*root; int main() { string str,s; int id; node *cur,*tmp; int kase=1; while(cin>>str) { bool decodable=1; root=new node(); cur=root; int i; for( i=0;i<str.size()-1;i++) { id=str[i]-'0'; if(cur->nxt[id]==NULL) { cur->nxt[id]=new node(); } cur=cur->nxt[id];
12293
The solution can be found by observation that if one player finds the total number of ball that is equal (2^n -1) then the player is looser. i.e. we find for n=3 it is true, so, if n=3 1st player looses, for n=4 1st player wins n=5 1st player wins, for n=6 1st player wins, for n=7 1st player
11311
#include <stdio.h> #include <iostream> using namespace std; int main() { int n,a,b,c,d,aa,bb,ans,t; cin>>t; while(t–) { cin>>a>>b>>c>>d; aa=a-c-1; bb=b-d-1; ans=aa^bb^c^d; if(ans==0) cout<<"Hansel\n"; else cout<<"Gretel\n"; } return 0; }
11716
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <string> using namespace std; int main() { int t,l,m; string str; cin>>t; getchar(); while(t–) { char gr[105][105]; getline(cin,str); l=str.size(); m=sqrt(l); if(m*m!=l) { cout<<"INVALID\n"; } else { int k=0; for(int i=0;i<m;i++) { for(int j=0;j<m;j++) { gr[i][j]=str[k]; k+=1; } } for(int i=0;i<m;i++) { for(int j=0;j<m;j++) cout<<gr[j][i]; } cout<<"\n";
10165
Nim game: It is an ancient game.Charles Boute discovered the trick behind the game strategy to win obviously. The idea is to figure out the current state of the stones of all the piles. If it is in the zero state then there is no way we can make a move to put the state
10703
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #define INF 1000000 using namespace std; void swp(int *p,int *q) { int tmp; tmp=*p; *p=*q; *q=tmp; } int main() { int h,w,n; int a,b,c,d; bool gr[505][505]; while(cin>>h) { cin>>w>>n; if(h+w+n==0) break; memset(gr,0,sizeof(gr)); int cnt=0; for(int k=0;k<n;k++) { cin>>a>>b>>c>>d; if(a>c) swp(&a,&c); if(b>d) swp(&b,&d); for(int i=a;i<=c;i++) { for(int j=b;j<=d;j++)
496
Using set::set_intersect(params) the problem can be solved. #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <set> #include <algorithm> #include <sstream> #define INF 1000000 using namespace std; set<int> Set1,Set2,inn; int main() { int n; string str; stringstream ss; while(getline(cin,str)) { Set1.clear(); Set2.clear(); inn.clear(); ss.clear(); ss<<str; while(ss>>n) { Set1.insert(n); } getline(cin,str); ss.clear(); ss<<str; while(ss>>n) { Set2.insert(n);
10771
This is a very problem. Explanation: Elimination continues till there is a single maid remains. If number of keka maids are odd at beginning it never becomes even through the elimination process so it will reach at 1 first. And if the number of keka maids starts with even number it never becomes odd according
11565
#include <iostream> #include <cmath> #include <cstdio> #include <vector> #include <algorithm> using namespace std; vector<int> vb; int main() { int t; int A,B,C; cin>>t; while(t–) { cin>>A>>B>>C; bool found=0; vb.clear(); for(int i=-100;i<=100;i++) { for(int j=-100;j<=100;j++) { for(int k=-100;k<=100;k++) { if(i+j+k==A) if(i*j*k==B) if(i*i+j*j+k*k==C) if(i!=j && i!=k && j!=k) { vb.push_back(i);vb.push_back(j);vb.push_back(k); found=1; i=101;j=101;k=101; } } } } if(found)