Skip to content

Commit af3923d

Browse files
author
Ahmad Medhat Othman
committed
codejam all the time
0 parents  commit af3923d

File tree

197 files changed

+221095
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

197 files changed

+221095
-0
lines changed

2009/gcj-R1A/A/A.cpp

+209
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
/**
2+
* @Author :Ahmed M. Osman
3+
* @CodeJame userName : AMedOs
4+
5+
* */
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
#include <iostream>
9+
#include <vector>
10+
#include <fstream>
11+
#include <sstream>
12+
#include <map>
13+
#include <string>
14+
#include <string.h>
15+
#include <algorithm>
16+
17+
using namespace std;
18+
19+
#define pb push_back
20+
#define all(v) v.begin(),v.end()
21+
#define sz size()
22+
#define loop(i,m) for(unsigned int i=0;i<m;i++)
23+
#define loop2(i,m) for(i=0;i<m;i++)
24+
#define loop4(i,x,m) for(unsigned int i=x;i<m;i++)
25+
#define loop4t(i,x,m) for(unsigned int i=x;i<=m;i++)
26+
#define mem(a,b) memset(a,b,sizeof(a))
27+
#define mp make_pair
28+
29+
typedef stringstream ss;
30+
typedef pair<int, int> pii;
31+
typedef vector<pii> vpii;
32+
typedef vector<string> vs;
33+
typedef vector<int> vi;
34+
typedef vector<vector<int> > vii;
35+
typedef long long ll;
36+
37+
38+
ifstream fin;
39+
ofstream fout;
40+
41+
int best[11][1000];
42+
int countt;
43+
/*
44+
string d_to_base(long n, int tbase){
45+
46+
ss strs;
47+
strs << n;
48+
string nstr = strs.str();
49+
if(tbase == 10)return nstr;
50+
51+
long n2 =n;
52+
string res="";
53+
54+
while (n2 > 0){
55+
ss strs;
56+
cout<<n2<<endl;
57+
cout<<res<<endl;
58+
strs << n2 % tbase;
59+
res += strs.str();
60+
n2 /= tbase;
61+
}
62+
cout << n <<" "<<tbase <<" "<< res<<endl;
63+
return res;
64+
}
65+
*/
66+
/*long base_to_d(string n, int fbase){
67+
return 1;
68+
}
69+
string c_base(long n,int fbase,int tbase){
70+
ss strs;
71+
strs << n;
72+
string nstr = strs.str();
73+
74+
if(fbase == tbase) return nstr;
75+
76+
if (fbase == 10){
77+
return d_to_base(n,tbase);
78+
}
79+
80+
if(tbase == 10){
81+
ss strs2;
82+
strs2 << base_to_d(nstr,fbase);
83+
return strs2.str();
84+
}
85+
return d_to_base(base_to_d(nstr,fbase),tbase);
86+
}*/
87+
/*
88+
bool ishappy3(long n,int base){
89+
int res = 0;
90+
int tmp;
91+
ss strs;
92+
strs<<n;
93+
string str = strs.str();
94+
loop(i,str.sz){
95+
tmp=atoi(str.substr(i,1).c_str());
96+
res+=tmp*tmp;
97+
}
98+
cout <<"happy 3 "<<res<<endl;
99+
if(res==1)
100+
return true;
101+
else {
102+
return false;
103+
}
104+
}
105+
106+
bool ishappy2(long n,int base){
107+
int res = 0;
108+
int tmp;
109+
ss strs;
110+
strs<<n;
111+
string str = strs.str();
112+
loop(i,str.sz){
113+
tmp=atoi(str.substr(i,1).c_str());
114+
res+=tmp*tmp;
115+
}
116+
cout <<"happy 2 "<<res<<endl;
117+
if(res==1)
118+
return true;
119+
else {
120+
return ishappy3(atoi(d_to_base(res,base).c_str()),base);
121+
}
122+
}
123+
*/
124+
bool ishappy(long n,int base){
125+
126+
if(n < 1000){
127+
if(best[base][n] == -2 ){
128+
best[base][n] = 0;
129+
return false;
130+
}
131+
if(best[base][n] != -1 )return best[base][n];
132+
}
133+
134+
int res = 0;
135+
long n2=n;
136+
while(n > 0){
137+
long rem = n%base;
138+
res += rem * rem;
139+
n/=base;
140+
}
141+
142+
if (res == 1){
143+
if(n2 < 1000)best[base][n2] = 1;
144+
return true;
145+
}
146+
else {
147+
if(n2 < 1000)best[base][n2] = -2;
148+
bool ret = ishappy(res,base);
149+
if(ret){
150+
if(n2 < 1000)best[base][n2] = 1;
151+
return true;
152+
}
153+
else{
154+
if(n2 < 1000)best[base][n2] = 0;
155+
return false;
156+
}
157+
}
158+
}
159+
160+
int main (){
161+
fin.open ("A-large-practice.in");
162+
fout.open ("test.out");
163+
164+
unsigned int cases;
165+
vi bases;
166+
string str;
167+
168+
fin >> cases;
169+
170+
getline(fin,str);
171+
172+
loop4t(ncase,1,cases){
173+
getline(fin,str);
174+
175+
bases.clear();
176+
177+
ss strs(str);
178+
int mm;
179+
while (strs >> mm){
180+
bases.pb(mm);
181+
}
182+
183+
//mem(best,-1);
184+
loop(i,11){
185+
loop(j,1000)
186+
j==1?best[i][j]=1:best[i][j]=-1;
187+
}
188+
189+
long n;
190+
for(n=2 ;; n++){
191+
192+
unsigned int i;
193+
loop2(i,bases.sz){
194+
countt = 0;
195+
//cout << best[2][2] <<endl;
196+
//if(n==1038217)cout << n << " "<<bases[i]<<endl;
197+
if(!ishappy(n,bases[i]))break;
198+
}
199+
if(i >= bases.sz)break;
200+
}
201+
fout << "Case #"<<ncase <<": "<<n<<endl;
202+
}
203+
204+
fin.close();
205+
fout.flush();
206+
fout.close();
207+
208+
return 0;
209+
}

2009/gcj-R1A/A/test.in

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
3
2+
2 3
3+
2 3 7
4+
9 10

2009/gcj-R1B/A/A.cpp

+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/**
2+
* @Author :Ahmed M. Osman
3+
* @CodeJame userName : AMedOs
4+
5+
6+
* */
7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
#include <iostream>
10+
#include <vector>
11+
#include <fstream>
12+
#include <sstream>
13+
#include <map>
14+
#include <string>
15+
#include <string.h>
16+
#include <algorithm>
17+
#include <cmath>
18+
#include <math.h>
19+
20+
using namespace std;
21+
22+
#define pb push_back
23+
#define all(v) v.begin(),v.end()
24+
#define sz(X) ((int)(X.size()))
25+
#define loop(i,m) for(int i=0;i<m;i++)
26+
#define loop2(i,m) for(i=0;i<m;i++)
27+
#define loop4(i,x,m) for(int i=x;i<m;i++)
28+
#define loop4t(i,x,m) for(int i=x;i<=m;i++)
29+
#define mem(a,b) memset(a,b,sizeof(a))
30+
#define mp make_pair
31+
const double pi=acos(-1.0);
32+
const double eps=1e-11;
33+
34+
typedef stringstream ss;
35+
typedef pair<int, int> pii;
36+
typedef vector<pii> vpii;
37+
typedef vector<string> vs;
38+
typedef vector<vs> vvs;
39+
typedef vector<int> vi;
40+
typedef vector<vector<int> > vvi;
41+
typedef long long ll;
42+
43+
44+
ifstream fin;
45+
ofstream fout;
46+
vs lines;
47+
string lstr;
48+
vector< pair<string,vs> > ad;
49+
int L;
50+
int A;
51+
double p = 1.0;
52+
int pos=0;
53+
void readtree(int an){
54+
55+
while(lstr[pos] == ' '){
56+
pos++;
57+
}
58+
59+
pos++;//(
60+
61+
double weight=0.0;
62+
string str="";
63+
while(lstr[pos] == ' '){
64+
pos++;
65+
}
66+
while(lstr[pos] != ' '){
67+
str +=lstr[pos++];
68+
}
69+
weight = atof(str.c_str());
70+
71+
p *= weight;
72+
//cout<< weight<<endl;
73+
74+
while(lstr[pos] == ' '){
75+
pos++;
76+
}
77+
str="";
78+
while(lstr[pos] != ' '){
79+
str +=lstr[pos++];
80+
}
81+
if (hasad(str,an)){
82+
readTree(an);
83+
readTree(an);
84+
}
85+
else{
86+
escaptree();
87+
escaptree();
88+
}
89+
90+
91+
92+
readTree();
93+
}
94+
95+
void solve(){
96+
readtree();
97+
}
98+
99+
int main (){
100+
fin.open ("A.in");
101+
fout.open ("A.out");
102+
103+
int tcases;
104+
string tstr;
105+
float tf=0.0;
106+
//int tn=0;
107+
108+
fin >> tcases;
109+
110+
loop4t(ncase,1,tcases){
111+
p = 1.0;
112+
113+
fin >> L;
114+
getline(fin,tstr);
115+
116+
lines.clear();
117+
lstr="";
118+
loop(line,L){
119+
getline(fin,tstr);
120+
lines.pb(tstr);
121+
lstr +=tstr;
122+
}
123+
124+
125+
fin >> A;
126+
127+
ad.clear();
128+
loop(i,A){
129+
tstr="";
130+
int n=0;
131+
132+
fin >> tstr >> n;
133+
vs desc;
134+
desc.clear();
135+
string str1="";
136+
loop(j,n){
137+
fin >> str1 ;
138+
desc.pb(str1);
139+
}
140+
141+
ad.pb(mp(tstr,desc));
142+
143+
/*cout << ad[i].first <<" ";
144+
loop(j,n){
145+
cout << ad[i].second[j] <<" ";
146+
}
147+
cout<<endl;*/
148+
}
149+
150+
pos=0;
151+
152+
fout << "Case #"<<ncase <<":"<<endl;
153+
loop(i,A){
154+
readtree(i);
155+
}
156+
157+
//fout <<lstr<<endl;
158+
159+
160+
tf = 0.5;
161+
//fout.precision(7);
162+
fout.width(9);
163+
fout.fill('0');
164+
fout <<left<<tf<<endl;
165+
}
166+
167+
fin.close();
168+
fout.flush();
169+
fout.close();
170+
171+
return 0;
172+
}

0 commit comments

Comments
 (0)