Skip to content

Instantly share code, notes, and snippets.

@rptynan
Created February 23, 2014 16:53
Show Gist options
  • Save rptynan/9173903 to your computer and use it in GitHub Desktop.
Save rptynan/9173903 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
int main(){
int N;
int a, b, c;
cin>>N;
for(int i=0; i<N; ++i){
cin>>a>>b>>c;
if(a==0 && b==0 && c==0){
cout<<"Over 9000 solutions"<<endl;
continue;
}
if(a==0 && b==0){
cout<<"No solution"<<endl;
continue;
}
if(a==0){
cout<<"1 solution"<<endl;
continue;
}
int dis = (b*b)-(4*a*c);
if(dis<0){
cout<<"No solution"<<endl;
continue;
}
if(dis==0){
cout<<"1 solution"<<endl;
continue;
}
else{
cout<<"2 solutions"<<endl;
continue;
}
}
return 0;
}
#include <iostream>
#include <string>
using namespace std;
#define INF 200000000
int R, C, room[100][100], orgroom[100][100];
string tin;
bool fail;
void flood(int row, int c, int lastdir, int u, int d, int l, int r){
if(fail==1) return;
if(row<0 || c<0) return;
if(row>R-1 || c>C-1) return;
if(room[row][c]==-1) return;
int dis = (int)u+(int)d+(int)l+(int)r;
if(dis>=room[row][c]) return;
if(dis<room[row][c]){
room[row][c]=dis;
}
//up
if(lastdir!=1){
flood(row-1,c,0,1,d,l,r);
}
//down
if(lastdir!=0){
flood(row+1,c,1,u,1,l,r);
}
//left
if(lastdir!=3){
flood(row,c-1,2,u,d,1,r);
}
//right
if(lastdir!=2){
flood(row,c+1,3,u,d,l,1);
}
return;
}
void copyorg(){
for(int r=0; r<R; ++r){
for(int c=0; c<C; ++c){
room[r][c]=orgroom[r][c];
}
}
}
void clearorg(){
for(int r=0; r<R; ++r){
for(int c=0; c<C; ++c){
orgroom[r][c]=0;
}
}
}
void print(int ar[100][100] ){
for(int r=0; r<R; ++r){
for(int c=0; c<C; ++c){
if(ar[r][c]==INF)
cout<<"I";
else if(ar[r][c]==-1)
cout<<"w";
else
cout<<ar[r][c];
}cout<<endl;
}cout<<endl;
}
int main(){
int N;
cin>>N;
for(int n=0; n<N; ++n){
cin>>R>>C;
clearorg();
fail=0;
//input
for(int r=0; r<R; ++r){
cin>>tin;
//cout<<tin<<endl;
for(int c=0; c<C; ++c){
if(tin[c]=='.'){
orgroom[r][c]=-1;
}
else{
orgroom[r][c]=INF;
}
}
}
copyorg();
for(int r=0; r<R; ++r){
for(int c=0; c<C; ++c){
if(room[r][c]==INF){
copyorg();
flood(r,c,-1,0,0,0,0); //0up,1down,2left,3right
// print(room);
for(int r1=0; r1<R; ++r1){
for(int c1=0; c1<C; ++c1){
if(room[r1][c1]>2){
fail=1;
goto fin;
}
}
}
copyorg();
}
}
}
fin:
cout<<"Room "<<n+1<<": "<<(fail?"NO":"YES")<<endl;
}
return 0;
}
#include <iostream>
using namespace std;
int S, N;
int W[150], V[150];
int K[151][151];
int main(){
cin>>S>>N;
for(int n=0; n<N; ++n){
cin>>W[n]>>V[n];
}
for(int n=0; n<=N; ++n){
for(int w=0; w<=S; ++w){
if(n==0 || w==0){
K[n][w]=0;
}
else K[n][w]=K[n-1][w];
if( W[n-1]<=w && K[n][w-W[n-1]]+V[n-1] > K[n][w] ){
K[n][w]=K[n][w-W[n-1]]+V[n-1];
}
}
}
/*for(int n=0; n<N; ++n){
for(int w=0; w<=S; ++w){
cout<<K[n][w];
} cout<<endl;}*/
cout<<K[N-1][S]<<endl;
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
int total=0;
int N;
int main(){
cin>>N;
if(N==3){ cout<<0<<endl; return 0;}
if(N==4){ cout<<1<<endl; return 0;}
if(N==5){ cout<<5<<endl; return 0;}
int nlines;
total=1;
int i;
for(int n=5; n<=N; ++n){
nlines = n-3;
//total+=nlines;
int toadd=n-3;
for(i=1; i <= (nlines/2); ++i){
total+=2*(toadd);
toadd+=(nlines-1)-i;
for(int o=0; o<i-1; ++o){
toadd-=1;
}
}
if(n%2==0){
total+=(toadd);
}
}
cout<<total<<endl;
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
int total=0;
int N;
int main(){
cin>>N;
if(N==3){ cout<<0<<endl; return 0;}
if(N==4){ cout<<1<<endl; return 0;}
if(N==5){ cout<<5<<endl; return 0;}
int nlines;
total=1;
int i;
for(int n=5; n<=N; ++n){
nlines = n-3;
//total+=nlines;
int toadd=n-3;
for(i=1; i <= (nlines/2); ++i){
total+=2*(toadd);
toadd+=nlines-1-i;
}
if(n%2==0){
total+=(toadd);
}
}
cout<<total<<endl;
return 0;
}
from math import factorial
N=int(input())
if(N<=3):
print(0)
else:
print( int((factorial(N)) / (factorial(4)*factorial(N-4))) )
#include <iostream>
using namespace std;
bool blocked[400][400];
int up[400][400], lef[400][400];
int R, C, res=0;
void resultify(int r, int c){
if(res < 2*r + 2*c ){
res=2*r + 2*c;
}
}
int main(){
cin>>R>>C;
string tin;
for(int r=0; r<R; ++r){
cin>>tin;
for(int c=0; c<C; ++c){
if(tin[c]=='X') blocked[r][c]=1;
else blocked[r][c]=0;
}
}
for(int r=0; r<R; ++r){
for(int c=0; c<C; ++c){
if(blocked[r][c])
continue;
if(r==0){
up[r][c]=1;
}
else{
up[r][c]=up[r-1][c]+1;
}
if(c==0){
lef[r][c]=1;
}
else{
lef[r][c]=lef[r][c-1]+1;
}
}
}
int t;
for(int r=0; r<R; ++r){
for(int c=0; c<C; ++c){
t=lef[r][c];
for(int r1=0; r1<up[r][c]; ++r1){
t=min(lef[r-r1][c],t);
resultify(r1+1,t);
}
}
}
cout<<res-1<<endl;
return 0;
}
#include <iostream>
using namespace std;
bool blocked[400][400];
int up[400][400], lef[400][400];
int R, C;
int main(){
cin>>R>>C;
string tin;
for(int r=0; r<R; ++r){
cin>>tin;
for(int c=0; c<C; ++c){
if(tin[c]=='X') blocked[r][c]=1;
else blocked[r][c]=0;
}
}
for(int r=0; r<R; ++r){
for(int c=0; c<C; ++c){
if(blocked[r][c])
continue;
if(r==0){
up[r][c]=1;
}
else{
up[r][c]=up[r-1][c]+1;
}
if(c==0){
lef[r][c]=1;
}
else{
lef[r][c]=lef[r][c-1]+1;
}
}
}
int res=0;
for(int r=0; r<R; ++r){
for(int c=0; c<C; ++c){
if(res < 2*up[r][c] + 2*lef[r][c] ){
res=2*up[r][c] + 2*lef[r][c];
}
}
}
cout<<res-1<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment