├── .gitignore ├── README.md ├── codechef ├── BOJACK.cpp ├── CHEFCAKE.cpp ├── CHEFCOMP.cpp ├── CHEFNSWAP.cpp ├── FIRESC.cpp ├── INF16I.cpp ├── ORTHODOX.cpp ├── XORCIST.cpp ├── a.exe ├── ababaaba.cpp ├── adashop2.cpp ├── admag.cpp ├── adtri.cpp ├── alextask.cpp ├── anudtc.cpp ├── anuund.cpp ├── asmgame1.cpp ├── baadshah.cpp ├── banrob.cpp ├── binop.cpp ├── bombing.cpp ├── camc.cpp ├── candy.cpp ├── capimove.cpp ├── carsell.cpp ├── catsdogs.cpp ├── cballs.cpp ├── chcoinsg.cpp ├── chefapar.cpp ├── chefarrp.cpp ├── chefezq.py ├── chefihg.cpp ├── chefinsq.py ├── cheflcm.cpp ├── chefmovr.cpp ├── chefpdig.cpp ├── chefspl.cpp ├── chefstlt.cpp ├── chefsum.cpp ├── chfnfrn.cpp ├── chpintu.cpp ├── chrl1.py ├── chrl2.cpp ├── chsqr.cpp ├── cleanup.cpp ├── conflip.cpp ├── covidlq.cpp ├── cprem.cpp ├── cvdrun.py ├── devarray.cpp ├── digitsep.cpp ├── donuts.cpp ├── easyprob.cpp ├── egrcake.cpp ├── engxor.cpp ├── entexam.cpp ├── fibeasy.rs ├── flow010.c ├── flow014.c ├── frgtnlng.cpp ├── germande.cpp ├── horses.cpp ├── hso8test.cpp ├── inf16e.cpp ├── input.txt ├── johny.cpp ├── kbignumb.cpp ├── knpsk.py ├── matches.cpp ├── maxisum.cpp ├── mfreq.cpp ├── minperm.cpp ├── mmprod.cpp ├── mnmx.cpp ├── mxmedian.cpp ├── numgame.cpp ├── p1z2s.cpp ├── posand.py ├── ppsum.py ├── rainbowa.cpp ├── rectsq.cpp ├── reservoi.cpp ├── rgame.cpp ├── samesnak.cpp ├── schedule.cpp ├── snakproc.cpp ├── strpalin.cpp ├── sumtrian.cpp ├── template.cpp ├── templela.c ├── tenplela.cpp ├── tourists.cpp ├── trisq.cpp ├── twostr.cpp ├── unitgcd.cpp ├── voters.cpp └── zco14001.cpp ├── codeforces ├── A │ ├── 1065a.cpp │ ├── 118a.cpp │ ├── 1208a.cpp │ ├── 158a.cpp │ ├── 1a.cpp │ ├── 236a.cpp │ ├── 4a.cpp │ ├── 50a.cpp │ ├── 540a.cpp │ ├── 651a.cpp │ ├── 71a.cpp │ ├── 732a.cpp │ ├── 845a.cpp │ └── 864a.cpp ├── B │ ├── 1042b.cpp │ ├── 1051b.cpp │ ├── 131b.cpp │ ├── 577b.cpp │ ├── 624b.cpp │ ├── 764b.cpp │ ├── 869b.cpp │ └── 913B.cpp ├── C │ ├── 1214C.cpp │ ├── 1426C.cpp │ ├── 466c.cpp │ ├── 4C.cpp │ ├── 4c.cpp │ ├── 519C.cpp │ ├── 519c.cpp │ ├── 546C.cpp │ ├── 546c.cpp │ ├── 550C.cpp │ ├── 550c.cpp │ ├── 580C.cpp │ ├── 580c.cpp │ ├── 665C.cpp │ ├── 735C.cpp │ ├── 735c.cpp │ ├── 747C.cpp │ ├── 747c.cpp │ ├── 755C.cpp │ └── 893C.cpp ├── D │ ├── 1106D.cpp │ └── 246D.cpp └── E │ └── 865e.cpp ├── spoj ├── a.exe ├── abcdef.cpp ├── abcpath.cpp ├── absys.cpp ├── acode.cpp ├── acpc10a.cpp ├── acpc10d.cpp ├── acpc11b.cpp ├── acquire.cpp ├── addrev.cpp ├── ae00.cpp ├── aggrcow.cpp ├── alicesie.cpp ├── amr10c.cpp ├── amr10g.cpp ├── amr11e.cpp ├── amri2d.cpp ├── anarc05b.cpp ├── anarc09a.cpp ├── ap2.cpp ├── arith2.java ├── army.cpp ├── arraysub.cpp ├── babtwr.cpp ├── beenums.cpp ├── binstirl.cpp ├── bishops.java ├── bitmap.cpp ├── bomber.cpp ├── brckts.cpp ├── brkstrng.cpp ├── bsheep.cc ├── buglife.cpp ├── busyman.cpp ├── byecakes.cpp ├── bytese2.cpp ├── bytesm2.cpp ├── cadydist.cpp ├── cam5.cpp ├── candy.cpp ├── candy3.cpp ├── canton.cpp ├── coins.cpp ├── comdiv.cpp ├── countmsts.cpp ├── cprmt.cpp ├── crds.cpp ├── ctrick.cpp ├── danger.cpp ├── datingrishi.cpp ├── diehard.cpp ├── disubstr.cpp ├── dotaa.cpp ├── doublevi.cpp ├── easudoku.cpp ├── ec_conb.cpp ├── edist.cpp ├── egypizza.cpp ├── eights.cpp ├── enigmath.cpp ├── etf.cpp ├── etf.py ├── facefrnd.cpp ├── farida.cpp ├── fashion.cpp ├── fctrl.cpp ├── fctrl2.cpp ├── fibosum.cpp ├── fplan.cpp ├── gcd2.py ├── gljive.cpp ├── gss1.cpp ├── gss3.cpp ├── gss4.cpp ├── gss5.cpp ├── gssiv.cpp ├── hackrndm.cpp ├── hangover.cpp ├── highways.cpp ├── histogra.cpp ├── horrible.cpp ├── hotels.cpp ├── hubullu.cpp ├── hypnos.cpp ├── icandies.cpp ├── incseq.cpp ├── input.txt ├── invcnt.cpp ├── invent.cpp ├── julka.cpp ├── kgss.cpp ├── kopc12a.cpp ├── kquery.cpp ├── labyr1.cpp ├── lastdig.cpp ├── lastdig2.cpp ├── lengfact.cpp ├── lisii.cpp ├── madoddsum.cpp ├── major.cpp ├── marbles.cpp ├── matsum.cpp ├── maxln.cpp ├── mcoins.cpp ├── menu.cpp ├── mflar10.cpp ├── micemaze.cpp ├── miserman.cpp ├── mixtures.cpp ├── mkthnum.cpp ├── mrecaman.cpp ├── mutdna.cpp ├── najpf.cpp ├── nhay.cpp ├── nmg.cpp ├── nsteps.cpp ├── numtsn.cpp ├── ny10a.cpp ├── ny10e.cpp ├── offside.cpp ├── ololo.cpp ├── onezero.cpp ├── onp.cpp ├── palin.cpp ├── party.cpp ├── permut2.cpp ├── phonelst.cpp ├── pigbank.cpp ├── pour1.cpp ├── ppath.cpp ├── prhyme.cpp ├── prime1.cpp ├── pto7y.cpp ├── qtree.cpp ├── qtree2.cpp ├── qtreev.cpp ├── quadarea.cpp ├── rent.cpp ├── rplc.cpp ├── samer08f.cpp ├── sbank.cpp ├── silver.cpp ├── smpdiv.cpp ├── sortout.cpp ├── stamps.cpp ├── stpar.cpp ├── submerge.cpp ├── sumitr.cpp ├── svada.cpp ├── swapdiffone.cpp ├── swaps.cpp ├── template.cpp ├── tesser.cpp ├── test.cpp ├── toandfro.cpp ├── topoland.cpp ├── tourist.cpp ├── trgrid.cpp ├── tricount.cpp ├── trt.cpp ├── twosqrs.cpp ├── vfmul.cpp └── willitst.cpp └── topcoder └── anagrams.java /.gitignore: -------------------------------------------------------------------------------- 1 | a.out 2 | a.exe 3 | codechef/a.out 4 | codechef/a.exe 5 | codeforces/a.out 6 | codeforces/a.exe 7 | spoj/a.out 8 | spoj/a.exe -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Competitive-Programming 2 | Solutions of competitive programming problems on various websites. 3 | 4 | Contribution Guidelines: 5 | 1. The name of the file is the problem code on the respective website. 6 | 2. The name of the file should be strictly in the lower case letters. 7 | 3. Don't add your solution if the solution to the same problem already exists in the repo. However, if your solution is in a different programming language, feel free to add them. 8 | 4. Attach a screenshot of the successful code submission in your PR. 9 | 10 | Very few codes maybe incomplete or incorrect. Kindly create an issue to correct them if you find one. 11 | -------------------------------------------------------------------------------- /codechef/CHEFCAKE.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #define fin for(int i=0;i 8 | #define vc vector 9 | #define vii vector 10 | int factorial(int n) 11 | { 12 | // single line to find factorial 13 | return (n == 1 || n == 0) ? 1 : n * factorial(n - 1); 14 | } 15 | int main() 16 | { 17 | ios_base::sync_with_stdio(false); 18 | cin.tie(NULL); 19 | int t; 20 | cin>>t; 21 | while(t--){ 22 | int n; 23 | cin>>n; 24 | ll sum=0; 25 | fin{ 26 | int val; 27 | cin>>val; 28 | sum+=val; 29 | } 30 | sum*=factorial(n-1); 31 | ll ans=((pow(10,n)-1)/9)*sum; 32 | cout< 2 | using namespace std; 3 | #define ll long long 4 | int main(){ 5 | ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL); 6 | ll t,n; 7 | cin>>t; 8 | while(t--){ 9 | cin>>n; 10 | ll sumf,count,sumn,i,half,diff; 11 | sumf=(n*(n+1))/2; 12 | count=0; 13 | if(sumf%2==0){ 14 | half=sumf/2; 15 | i=sqrt(sumf); 16 | sumn=(i*(i+1))/2; 17 | if(sumn>half) 18 | sumn-=i,i--; 19 | diff=half-sumn; 20 | while(diff=0){ 21 | if(diff==0){ 22 | count+=(i*(i-1))/2; 23 | ll m=n-i; 24 | count+=(m*(m-1))/2; 25 | } 26 | else if(diffi) 28 | count+=min(n-diff,i); 29 | else 30 | count+=min(n-i,diff); 31 | } 32 | diff+=i;i--; 33 | } 34 | cout< 2 | using namespace std; 3 | #define ll long long 4 | #define fin for(int i=0;i 8 | #define vc vector 9 | #define vii vector 10 | #define pb push_back 11 | #define mp make_pair 12 | #define mod 1000000007 13 | int vis[100005]; 14 | vi ar[100005]; 15 | int cc_size; 16 | void dfs(int node){ 17 | vis[node]=1; 18 | cc_size++; 19 | for(int child:ar[node]) 20 | if(vis[child]==0) 21 | dfs(child); 22 | 23 | } 24 | int main() 25 | { 26 | ios_base::sync_with_stdio(false); 27 | cin.tie(NULL); 28 | #ifndef ONLINE_JUDGE 29 | freopen("input.txt","r",stdin); 30 | freopen("output.txt","w",stdout); 31 | #endif 32 | int t; 33 | cin>>t; 34 | while(t--){ 35 | int n,m,a,b; 36 | cin>>n>>m; 37 | for(int i=1;i<=n;i++) vis[i]=0,ar[i].clear(); 38 | for(int i=1;i<=m;i++) cin>>a>>b , ar[a].pb(b),ar[b].pb(a); 39 | int cnt=0; 40 | ll res=1; 41 | for(int i=1;i<=n;i++){ 42 | if(vis[i]==0){ 43 | cc_size=0; 44 | dfs(i),cnt++; 45 | res=(res*cc_size) % mod; 46 | } 47 | } 48 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | int n,k; 9 | cin>>n>>k; 10 | vector d(n+1); 11 | for(int i=1;i<=n;i++){ 12 | int x,y; 13 | cin>>x>>y; 14 | d[x]=y; 15 | } 16 | vector size(n+1,0); 17 | for(int i=1;i<=n;i++){ 18 | cin>>size[i]; 19 | } 20 | vector us(k); 21 | for(int i=0;i>us[i]; 23 | } 24 | int count=0; 25 | for(int i=0;i 2 | using namespace std; 3 | #define ll long long 4 | #define fin for(int i=0;i 8 | #define vc vector 9 | #define vii vector 10 | int main() 11 | { 12 | ios_base::sync_with_stdio(false); 13 | cin.tie(NULL); 14 | #ifndef ONLINE_JUDGE 15 | freopen("input.txt","r",stdin); 16 | freopen("output.txt","w",stdout); 17 | #endif 18 | int t; 19 | cin>>t; 20 | ll a[100005]; 21 | while(t--){ 22 | int n,flag=0,j; 23 | set se; 24 | cin>>n; 25 | fin{ 26 | cin>>a[i]; 27 | } 28 | for(int i=0;i 2 | using namespace std; 3 | #define ll long long 4 | #define fin for(int i=0;i 8 | #define vc vector 9 | #define vii vectorint 10 | int main() 11 | { 12 | ios_base::sync_with_stdio(false); 13 | cin.tie(NULL); 14 | #ifndef ONLINE_JUDGE 15 | freopen("input.txt","r",stdin); 16 | freopen("output.txt","w",stdout); 17 | #endif 18 | ll t; 19 | ll a[1000005]; 20 | ll ans[100005]; 21 | cin>>t; 22 | while(t--){ 23 | ll n,q; 24 | cin>>n>>q; 25 | fin{ 26 | cin>>a[i]; 27 | } 28 | 29 | // for(int i=0;) 30 | while(q--){ 31 | ll l,r; 32 | cin>>l>>r; 33 | ll count=0; 34 | for(ll i=l;i<=r;i++) 35 | { 36 | for(ll j=i;j<=r;j++) 37 | { 38 | if((a[i]^a[j])>=l && (a[i]^a[j])<=r) 39 | count++; 40 | } 41 | } 42 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | string s; 9 | cin>>s; 10 | string ans = "None"; 11 | int a=0,b=0; 12 | for(int i=0; i 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | int count=0; 9 | long long int i=1,n,j=1; 10 | cin>>n; 11 | while(i<=n){ 12 | long long int temp=i; 13 | i+=j; 14 | j=temp; 15 | count++; 16 | } 17 | cout< 2 | using namespace std; 3 | 4 | vector prime(5000001,true); 5 | vector v(5000001,false); 6 | 7 | void sieve(){ 8 | prime[0]=prime[1]=false; 9 | for(int i=2;i*i<5000001;i++){ 10 | if(!prime[i]) continue; 11 | for(int j=i*i;j<5000001;){ 12 | prime[j]=false; 13 | j+=i; 14 | } 15 | } 16 | for(int i=2;i<5000001;i++){ 17 | if(!prime[i]) continue; 18 | if(i%4!=1) continue; 19 | for(int j=i;j<5000001;j+=i){ 20 | v[j]=true; 21 | } 22 | } 23 | } 24 | 25 | int main(){ 26 | int t; 27 | scanf("%d",&t); 28 | sieve(); 29 | while(t--){ 30 | int n; 31 | scanf("%d",&n); 32 | bool ans=v[n]; 33 | if(ans) printf("YES\n"); 34 | else printf("NO\n"); 35 | } 36 | } -------------------------------------------------------------------------------- /codechef/alextask.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long int gcd(long long int a,long long int b){ 5 | if(b>a) { 6 | long long int t=a; 7 | a=b; 8 | b=t; 9 | } 10 | while(b) {long long int t = a%b; a=b; b=t;} 11 | return a; 12 | } 13 | 14 | long long int lcm(long long int a,long long int b){ 15 | return a/gcd(a,b)*b; 16 | } 17 | 18 | 19 | int main(){ 20 | ios_base::sync_with_stdio(false); 21 | int t; 22 | cin>>t; 23 | for(int k=0;k>n; 26 | vector v(n); 27 | for(int i=0;i>v[i]; 28 | long long int min=LLONG_MAX; 29 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | for(int i=0;i>n; 10 | if(360%n==0) cout<<"y "; 11 | else cout<<"n "; 12 | if(n<=360) cout<<"y "; 13 | else cout<<"n "; 14 | if (n<27) cout<<"y "; 15 | else cout<<"n "; 16 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | int t; 7 | cin>>t; 8 | for(int i=0;i>n; 11 | int* a = new int[n]; 12 | for(int j=0;j>a[j]; 14 | } 15 | sort(a,a+n); 16 | int* b = new int[n]; 17 | for(int i=0,k=0;i 2 | using namespace std; 3 | 4 | int gcd(int x, int y){ 5 | int temp; 6 | if(x>t; 29 | for(int i=0;i>n; 32 | int* a = new int[n]; 33 | for(int j=0;j>a[j]; 34 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | long long int n,m; 6 | cin>>n>>m; 7 | vector v(n+1); 8 | for(int i=0;i>v[i]; 10 | } 11 | int len = (int)sqrt(n + 0.0) + 1; 12 | vector b (len,0); 13 | for (int i=0; i>x; 21 | if(x==1){ 22 | long long int p,q; 23 | cin>>p>>q; 24 | p--; 25 | v[p]=q; 26 | int j=p/len; 27 | b[j]=0; 28 | for(int i=j*len; i>s; 39 | int i=0; 40 | long long int sum=0; 41 | while(sum 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | int t; 7 | cin>>t; 8 | for(int i=0;i>a; 11 | cin>>b; 12 | bool one=false; 13 | bool zero=false; 14 | int n= a.size(); 15 | for(int j=0;jt1? t0:t1 ; // min of t0 or t1 -- corresponding to xor swaps 29 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | long long int n,x; 7 | cin>>n; 8 | vector v(n); 9 | for(int i=0;i>v[i]; 10 | for(int i=1;i>x; 16 | vector ans; 17 | for(int i=n-1;i>=0;i--){ 18 | if(v[i]<=x) { 19 | x-=v[i]; 20 | ans.push_back(v[i]); 21 | } 22 | } 23 | if(x==0){ 24 | for(int i=ans.size()-1;i>=0;i--){ 25 | //cout< 2 | using namespace std; 3 | 4 | typedef pair ii; 5 | 6 | int main(){ 7 | int t; 8 | cin>>t; 9 | while(t--){ 10 | int n; 11 | cin>>n; 12 | vector v; 13 | for(int i=1;i<=n;i++) { 14 | int temp; 15 | cin>>temp; 16 | v.push_back(ii(temp,i)); 17 | } 18 | sort(v.begin(), v.end()); 19 | set g[n+1]; 20 | for(int i=1;i>u>>vv; 23 | g[u].insert(vv); 24 | g[u].insert(u); 25 | g[vv].insert(u); 26 | g[vv].insert(vv); 27 | } 28 | vector ans(n+1,0); 29 | for(int i=1;i<=n;i++){ 30 | for(int j=n-1;j>=0;j--){ 31 | int x = v[j].second; 32 | if(g[i].find(x)==g[i].end()){ 33 | ans[i]=x; 34 | break; 35 | } 36 | } 37 | } 38 | for(int i=1;i<=n;i++) cout< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | int t; 7 | cin>>t; 8 | while(t--){ 9 | long long int n; 10 | cin>>n; 11 | vector v(n); 12 | for(int i=0;i>v[i]; 14 | } 15 | sort(v.begin(),v.end()); 16 | 17 | long long int total = 0; 18 | long long int mod = 1000000007; 19 | 20 | for(int i=n-1;i>=0;i--){ 21 | int depriciation = n -1 - i; 22 | total += max(v[i] - depriciation,0); 23 | total %= mod; 24 | } 25 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | for(int i=0;i>c>>d>>l; 10 | if(l%4) { 11 | cout<<"no"< 2*d) { 17 | l -= (c-2*d)*4; 18 | c = 2*d; 19 | } 20 | if(l>=0 && l <= 4*c) cout<<"yes"< 2 | using namespace std; 3 | 4 | int gcd(int a[]); 5 | 6 | int main(){ 7 | int t; 8 | cin>>t; 9 | for(int i=0;i>n; 12 | int* a= new int[n]; 13 | int count=0; //no of balls added 14 | for(int j=0;j>a[j]; 16 | } 17 | for(int j=1;j1) break; 27 | else { 28 | 29 | } 30 | }while(1); 31 | } 32 | return 0; 33 | } 34 | 35 | int gcd(int a[]){ 36 | 37 | 38 | 39 | return 40 | } 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /codechef/chcoinsg.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | for(int i=0;i>n; 10 | if(n%6==0) cout<<"Misha"< 2 | #define sz(a) int((a).size()) 3 | #define pb push_back 4 | #define all(c) (c).begin(),(c).end() 5 | #define tr(c,i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++) 6 | #define present(c,x) ((c).find(x) != (c).end()) 7 | #define cpresent(c,x) (find(all(c),x) != (c).end()) 8 | using namespace std; 9 | 10 | typedef long long int ll; 11 | typedef vector vi; 12 | typedef vector vvi; 13 | typedef pair ii; 14 | typedef vector vii; 15 | typedef vector vvii; 16 | 17 | int main(){ 18 | int t; 19 | cin>>t; 20 | while(t--){ 21 | int n; 22 | cin>>n; 23 | vi v(n+1); 24 | int first=0; 25 | int count=0; 26 | for(int i=1;i<=n;i++) { 27 | cin>>v[i]; 28 | if(!first && v[i]==0) first=i; 29 | if(!v[i]) count++; 30 | } 31 | if(!first) cout<<0< 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | for(int i=0;i>n; 10 | int count=n; // No. of sub array equals no of elements atleast. 11 | int *a; 12 | a=new int[n]; 13 | for(int j=0;j>a[j]; 15 | } 16 | 17 | for(int j=0;j 3 | using namespace std; 4 | 5 | #define BOOSTIO ios_base::sync_with_stdio (0), cin.tie (0) 6 | #define MOD 1e9 + 7 7 | #define LL long long 8 | #define LD long double 9 | #define FOR(i, a, b) for(int i = a; i < b; ++i) 10 | #define rFOR(i, a, b) for(int i = a; i > b; --i) 11 | #define PB push_back 12 | 13 | typedef vector Vector; 14 | typedef pair Pair; 15 | typedef map Map; 16 | typedef stack Stack; 17 | typedef queue Queue; 18 | typedef priority_queue PQ;; 19 | 20 | int n,m; 21 | char s[100001]; 22 | int k=0; 23 | 24 | 25 | void bfs(char** a,int i,int j){ 26 | a[i][j]='*'; 27 | if( j+1=0 && a[i][j-1]=='.'){ 36 | s[k++]='R'; 37 | bfs(a,i,j-1); 38 | } 39 | if( i-1>=0 && a[i-1][j]=='.'){ 40 | s[k++]='D'; 41 | bfs(a,i-1,j); 42 | } 43 | } 44 | 45 | 46 | 47 | int main () { 48 | BOOSTIO; 49 | 50 | cin>>n>>m; 51 | char** a = new char*[n]; 52 | int c_i,c_j; 53 | 54 | for(int i=0;i>a[i][j]; 59 | if(a[i][j]=='C') { 60 | c_i=i; 61 | c_j=j; 62 | } 63 | } 64 | } 65 | bfs(a,c_i,c_j); 66 | s[k++]='\0'; 67 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | for(int i=0;i>n; 10 | long long int sum=0; 11 | for(long long int i=1;i*i<=n;i++){ 12 | if(n%i==0) { 13 | sum+=i; 14 | sum+=n/i; 15 | if(i==n/i) sum-=i; 16 | } 17 | } 18 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | int n,d; 9 | cin>>n>>d; 10 | vector v(n); 11 | long long int sum=0; 12 | for(int i=0;i>v[i]; 14 | sum+=v[i]; 15 | } 16 | // long long int sum = accumulate(v.begin(), v.end(),0); 17 | bool ans = true; 18 | if(sum%n) ans = false; 19 | int count = 0; 20 | long long int f = sum/n; 21 | for(int i=0;if) v[i+d] += diff; 24 | else v[i+d] -= diff; 25 | v[i] = f; 26 | count += diff; 27 | } 28 | for(int i=n-d;i 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | string s; 9 | cin>>s; 10 | int n = s.size(); 11 | unordered_map v; 12 | for(int i=0;i<10;i++) v[i] = 0; 13 | for(int i=0;i ans; 15 | for(char i='A';i<='Z';i++){ 16 | int d1 = int(i)%10; 17 | int d2 = int(i)/10; 18 | if(d1!=d2 && v[d1]>0 && v[d2]>0) ans.push_back(i); 19 | else if(d1==d2 && v[d1]>1) ans.push_back(i); 20 | } 21 | for(int i=0;i 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main(){ 7 | int d; 8 | cin>>d; 9 | for(int i=0;i>s; 12 | int l= s.length(); 13 | if(s.length()==1){ 14 | cout<<"NO"< 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | string a,b; 9 | cin>>a>>b; 10 | int min=0,max=0; 11 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | int n; 9 | cin>>n; 10 | vector v(n); 11 | for(int i=0;i>v[i]; 12 | int min = 0; 13 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | int t; 7 | cin>>t; 8 | while(t--){ 9 | int n,m; 10 | cin>>n>>m; 11 | vector p(n),f(n),cost(m+1,-1); 12 | for(int i=0;i>f[i]; 13 | for(int i=0;i>p[i]; 14 | for(int i=0;i k: 10 | return knpsk(c, w, index-1, k) 11 | if index not in memo: 12 | memo[index] = dict() 13 | memo[index][k] = max(w[index]+knpsk(c, w, index-1, k-c[index]), knpsk(c, w, index-1, k)) 14 | return memo[index][k] 15 | tcase = int(input()) 16 | while tcase > 0: 17 | tcase -= 1 18 | c, w = [], [] 19 | n, k = map(int, input().split()) 20 | for i in range(n): 21 | cost, weight = map(int, input().split()) 22 | c.append(cost) 23 | w.append(weight) 24 | print(knpsk(c, w, n-1, k)) 25 | # initializing memo for upcoming testcases 26 | memo = dict() -------------------------------------------------------------------------------- /codechef/chrl2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | string s; 6 | cin>>s; 7 | std::vector c(4,0); 8 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | int t; 7 | cin>>t; 8 | for(int k=0;k>n; 11 | vector > v(n); 12 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | #define INF (int)1e9 6 | #define EPS 1e-9 7 | #define REP(i, a, b) for(int i = a; i < b; ++i) 8 | #define PB push_back 9 | #define MP make_pair 10 | 11 | typedef long long ll; 12 | typedef pair PII; 13 | // vector VI,chef,chefas; 14 | typedef vector VS; 15 | typedef vector VII; 16 | 17 | const float PI = 3.1415926535897932384626433832795; 18 | const int MOD = 1000000007; 19 | 20 | int main() 21 | { 22 | int t,i,j,n,m,temp; 23 | 24 | scanf("%d",&t); 25 | while(t--){ 26 | scanf("%d %d",&n,&m); 27 | vector VI,chef,chefas; 28 | for (i = 1; i <= n; i++) VI.push_back(i); 29 | 30 | for (i = 1; i <= m; i++){ 31 | scanf("%d",&temp); 32 | VI.erase(std::remove(VI.begin(), VI.end(), temp), VI.end()); 33 | } 34 | 35 | sort(VI.begin(),VI.end()); 36 | 37 | for(i=0; i < VI.size(); i++){ 38 | if (i%2==0){ 39 | chef.push_back(VI[i]); 40 | } 41 | else { 42 | chefas.push_back(VI[i]); 43 | } 44 | } 45 | for (i = 0; i < chef.size(); i++){ 46 | printf("%d ",chef[i]); 47 | } 48 | printf("\n"); 49 | for (i = 0; i < chefas.size(); i++){ 50 | printf("%d ",chefas[i]); 51 | } 52 | printf("\n"); 53 | } 54 | 55 | return 0; 56 | } -------------------------------------------------------------------------------- /codechef/conflip.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int t; 6 | cin>>t; 7 | for(int i=0;i>g; 12 | for(int j=0;j>r; 15 | cin>>n; 16 | cin>>q; 17 | if(n%2==0) 18 | { 19 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | int n; 9 | cin>>n; 10 | int counter = 0; 11 | int last = 0; 12 | bool ans = true; 13 | for(int i=0;i>current; 16 | if(last == 0 && current == 1 ){ 17 | last = 1; 18 | counter++; 19 | } else if (last == 0 && current == 0){ 20 | continue; 21 | } else if (last == 1 && current == 0){ 22 | counter++; 23 | } else { // last == 1 && current == 1 24 | if(counter<6) { 25 | ans = false; 26 | } 27 | else { 28 | counter = 1; 29 | } 30 | } 31 | } 32 | if(ans) cout<<"YES"< 2 | using namespace std; 3 | 4 | int m=1000000007; 5 | 6 | long long int power(long long int a,long long int b){ 7 | if(b==1) return a; 8 | else { 9 | long long int t = power(a,b/2)%m; 10 | t = (t*t)%m; 11 | if (b%2==1) t=(t*a)%m; 12 | return t; 13 | } 14 | } 15 | 16 | int main(){ 17 | ios_base::sync_with_stdio(false); 18 | int t; 19 | cin>>t; 20 | for(int k=0;k>n; 23 | if(n==1) { 24 | cout<<0< 2 | using namespace std; 3 | 4 | int main(){ 5 | int n,q; 6 | cin>>n>>q; 7 | int* a = new int[n]; 8 | int* b = new int[q]; 9 | for(int i=0;i>a[i]; 10 | for(int i=0;i>b[i]; 11 | int min=a[0]; 12 | int max=a[0]; 13 | for(int i=0;imax) max=a[i]; 15 | if(a[i]=min && b[i]<=max) cout<<"Yes"< 2 | using namspace std; 3 | 4 | int f(string s,int x,int y,int c,int i){ 5 | 6 | } 7 | 8 | int main(){ 9 | int t; 10 | cin>>t; 11 | while(t--){ 12 | int n,m,x,y; 13 | cin>>n; 14 | string s; 15 | cin>>s>>m>>x>>y; 16 | if(m==1){ 17 | int a= __gcd(s[0]-'0',s[i]-'0'); 18 | for(int i=2;i 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | int t; 7 | cin>>t; 8 | while(t--){ 9 | int n,m; 10 | cin>>n>>m; 11 | vector v(m); 12 | for(int i=0;i>v[i]; 14 | } 15 | sort(v.begin(), v.end()); 16 | int sum=0; 17 | int i; 18 | for(i=1;i<=m;i++){ 19 | sum+=v[i-1]; 20 | if(sum>=m-1-i){ 21 | break; 22 | } 23 | } 24 | if(sum==m-1-i) cout< 2 | using namespace std; 3 | int main() 4 | { 5 | cout<<"137=2(2(2)+2+2(0))+2(2+2(0))+2(0)\n"; 6 | cout<<"1315=2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)\n"; 7 | cout<<"73=2(2(2)+2)+2(2+2(0))+2(0)\n"; 8 | cout<<"136=2(2(2)+2+2(0))+2(2+2(0))\n"; 9 | cout<<"255=2(2(2)+2+2(0))+2(2(2)+2)+2(2(2)+2(0))+2(2(2))+2(2+2(0))+2(2)+2+2(0)\n"; 10 | cout<<"1384=2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2)+2(2(2)+2(0))+2(2+2(0))\n"; 11 | cout<<"16385=2(2(2+2(0))+2(2)+2)+2(0)\n"; 12 | return 0; 13 | } -------------------------------------------------------------------------------- /codechef/egrcake.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | int t; 7 | cin>>t; 8 | for(int i=0;i>n>>m; 11 | int* a = new int[n+1]; 12 | a[0]=-1; 13 | for(int j=1;j<=n-m;j++){ 14 | a[j]=j+m; 15 | } 16 | for(int j=n-m+1,i=1;j<=n;j++){ 17 | a[j]=i++; 18 | } 19 | int count=0; 20 | int crawler=1; 21 | while(true){ 22 | //cout< 2 | #include 3 | using namespace std; 4 | 5 | bool isEvenBits(int n){ 6 | int ones = 0; 7 | while(n>0){ 8 | if(n%2==1) ones++; 9 | n/=2; 10 | } 11 | if(ones%2 == 0 ) return true; 12 | else return false; 13 | } 14 | 15 | int countEven1s(vector &b){ 16 | int n=b.size(); 17 | int count=0; 18 | for(int i=0;i>t; 27 | while(t--){ 28 | int n,q; 29 | scanf("%d%d", &n, &q); 30 | vector a(n); 31 | for(int i=0;i pp(1000000+1); 38 | for(int i=pp.size()-1;i>0;i--){ 39 | pp[i] = isEvenBits(i); 40 | } 41 | while(q--){ 42 | int p; 43 | scanf("%d", &p); 44 | if(pp[p]){ 45 | printf("%d %d\n",c,n-c); 46 | } 47 | else { 48 | printf("%d %d\n",n-c,c); 49 | } 50 | } 51 | } 52 | return 0; 53 | } -------------------------------------------------------------------------------- /codechef/entexam.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void print(int a[], int n){ 6 | for(int i=0;i>t; 15 | for(int i=0;i>n>>ki>>e>>m; 18 | int* a = new int[n-1]; 19 | for(int j=0;j>temp2; 24 | temp+=temp2; 25 | } 26 | a[j]=temp; 27 | } 28 | int num=0; 29 | for(int k=0;k>temp; 32 | num+=temp; 33 | } 34 | //print(a,n-1); 35 | sort(a,a+n-1); 36 | //print(a,n-1); 37 | //cout<() -> T { 2 | let mut s = String::new(); 3 | std::io::stdin().read_line(&mut s).ok(); 4 | s.trim().parse().ok().unwrap() 5 | } 6 | 7 | fn ret_fib(x: u64) -> i32 { 8 | let x_mod_15 = x % 15; 9 | let x_dev_15_mod_4 = (x / 15) % 4; 10 | let num_mat: Vec> = vec![vec![0,1,1,2,3,5,8,3,1,4,5,9,4,3,7], 11 | vec![0,7,7,4,1,5,6,1,7,8,5,3,8,1,9], 12 | vec![0,9,9,8,7,5,2,7,9,6,5,1,6,7,3], 13 | vec![0,3,3,6,9,5,4,9,3,2,5,7,2,9,1]]; 14 | num_mat[x_dev_15_mod_4 as usize][x_mod_15 as usize] 15 | } 16 | 17 | fn main() { 18 | let t: u32 = read1(); 19 | for _ in 0..t { 20 | let n: u64 = read1(); 21 | let mut temp = n; 22 | let mut cnt: u64 = 1; 23 | while temp > 1 { 24 | temp /= 2; 25 | cnt *= 2; 26 | } 27 | println!("{}", ret_fib(cnt-1)); 28 | } 29 | } -------------------------------------------------------------------------------- /codechef/flow010.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | {int n,i; 4 | char ch; 5 | scanf("%d",&n); 6 | for(i=1;i<=n;i++) 7 | { 8 | scanf(" %c",&ch); 9 | if(ch=='B'||ch=='b') 10 | printf("BattleShip\n"); 11 | else if(ch=='C'||ch=='c') 12 | printf("Cruiser\n"); 13 | else if(ch=='D'||ch=='d') 14 | printf("Destroyer\n"); 15 | else if(ch=='F'||ch=='f') 16 | printf("Frigate\n"); 17 | } 18 | return 0;} -------------------------------------------------------------------------------- /codechef/flow014.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int h,t,n,i; 5 | double c; 6 | scanf("%d",&n); 7 | for(i=1;i<=n;i++){ 8 | scanf("%d%lf%d",&h,&c,&t); 9 | if(h>50&&c<0.7&&t>5600) 10 | printf("10\n"); 11 | else if(h>50&&c<0.7) 12 | printf("9\n"); 13 | else if(c<0.7&&t>5600) 14 | printf("8\n"); 15 | else if(h>50&&t>5600) 16 | printf("7\n"); 17 | else if(h>50||c<0.7||t>5600) 18 | printf("6\n"); 19 | else if(h<=50&&c>=0.7&&t<=5600) 20 | printf("5\n"); 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /codechef/frgtnlng.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | int n,k; 9 | cin>>n>>k; 10 | std::vector v; 11 | while(n--){ 12 | string s; 13 | cin>>s; 14 | v.push_back(s); 15 | } 16 | set st; 17 | while(k--){ 18 | int tt; 19 | cin>>tt; 20 | while(tt--){ 21 | string s; 22 | cin>>s; 23 | st.insert(s); 24 | } 25 | } 26 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | #define INF (int)1e9 6 | #define EPS 1e-9 7 | #define REP(i, a, b) for(int i = a; i < b; ++i) 8 | #define PB push_back 9 | #define MP make_pair 10 | 11 | typedef long long ll; 12 | typedef pair PII; 13 | typedef vector VI; 14 | typedef vector VS; 15 | typedef vector VII; 16 | 17 | const float PI = 3.1415926535897932384626433832795; 18 | const int MOD = 1000000007; 19 | 20 | int main() 21 | { 22 | int t,n; 23 | scanf("%d",&t); 24 | while(t--){ 25 | scanf("%d",&n); 26 | int arr[n]; 27 | for (int i = 0; i < n; i++) 28 | scanf("%d",&arr[i]); 29 | sort(arr,arr+n); 30 | 31 | int min = arr[1] - arr[0]; 32 | 33 | for (int i = 0; i < n-1; ++i){ 34 | int temp = arr[i+1] - arr[i]; 35 | if (temp 2 | int main(){ 3 | int amt; 4 | float bal; 5 | scanf("%d",&amt); 6 | scanf("%f",&bal); 7 | if(((float)amt+0.5)>bal || amt%5 !=0) 8 | printf("%.2f",bal); 9 | else 10 | printf("%.2f",bal-0.5-(float)amt); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /codechef/inf16e.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace sdt; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | int r,g; 8 | cin>>r>>g; 9 | long long int a,b; 10 | cin>>a>>b; 11 | vector > right(r); 12 | for(int i=0;i>p>>v; 15 | right[i].first = p; 16 | right[i].second = v; 17 | } 18 | vector > left(r); 19 | for(int i=0;i>p>>v; 22 | left[i].first = p; 23 | left[i].second = v; 24 | } 25 | } -------------------------------------------------------------------------------- /codechef/input.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 5 3 3 | 1 0 1 0 0 1 1 1 1 0 0 0 1 0 0 4 | -------------------------------------------------------------------------------- /codechef/johny.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define REP(i, a, b) for(int i = a; i < b; ++i) 6 | 7 | int main() 8 | { 9 | int t,n,i,j,k,val; 10 | cin >> t; 11 | while(t--){ 12 | cin >> n; 13 | int arr[n+1]; 14 | REP(i,1,n+1)scanf("%d",&arr[i]); 15 | scanf("%d",&k); 16 | val = arr[k]; 17 | sort(arr+1,arr+n+1); 18 | REP(i,1,n+1) 19 | if (val == arr[i]){ 20 | cout << i << endl; 21 | break; 22 | } 23 | } 24 | return 0; 25 | } -------------------------------------------------------------------------------- /codechef/kbignumb.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define sz(a) int((a).size()) 3 | #define pb push_back 4 | #define all(c) (c).begin(),(c).end() 5 | #define tr(c,i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++) 6 | #define present(c,x) ((c).find(x) != (c).end()) 7 | #define cpresent(c,x) (find(all(c),x) != (c).end()) 8 | using namespace std; 9 | 10 | typedef vector vi; 11 | typedef vector vvi; 12 | typedef pair ii; 13 | typedef vector vii; 14 | typedef vector vvii; 15 | typedef long long int ll; 16 | 17 | 18 | ll f(ll a,ll n, ll m, ll x){ 19 | if(n==1) return a%m; 20 | ll temp = f(a,n/2,m,x); 21 | temp = ((temp*x)%m + temp)%m; 22 | if(n%2) { 23 | temp = ((temp*x)%m + a)%m; 24 | } 25 | return temp; 26 | } 27 | 28 | 29 | int main(){ 30 | int t; 31 | cin>>t; 32 | while(t--){ 33 | ll a,n,m; 34 | cin>>a>>n>>m; 35 | ll x=1,temp=a; 36 | while(temp){ 37 | x*=10; 38 | temp/=10; 39 | } 40 | cout< 0: 22 | max2 = l2[0] 23 | k = True 24 | if len(l1) > 1: 25 | max1 = l1[0] + l1[1] 26 | elif len(l1) == 1: 27 | max1 = l1[0] 28 | k = False 29 | if max2 > max1: 30 | memo[i] = memo[i-2] + max2 31 | l2.pop(0) 32 | else: 33 | memo[i] = memo[i-2] + max1 34 | if k: 35 | l1.pop(0) 36 | l1.pop(0) 37 | else: 38 | l1.pop(0) 39 | if len(dupl1) > 0: 40 | memo[1] = dupl1[0] 41 | dupl1.pop(0) 42 | else: 43 | memo[1] = 0 44 | for i in range(3, s+1, 2): 45 | max2, max1 = 0, 0 46 | if len(dupl2) > 0: 47 | max2 = dupl2[0] 48 | k = True 49 | if len(dupl1) > 1: 50 | max1 = dupl1[0] + dupl1[1] 51 | elif len(dupl1) == 1: 52 | max1 = dupl1[0] 53 | k = False 54 | if max2 > max1: 55 | memo[i] = memo[i-2] + max2 56 | dupl2.pop(0) 57 | else: 58 | memo[i] = memo[i-2] + max1 59 | if k: 60 | dupl1.pop(0) 61 | dupl1.pop(0) 62 | else: 63 | dupl1.pop(0) 64 | for i in range(1, s+1): 65 | print(memo[i], end=" ") -------------------------------------------------------------------------------- /codechef/matches.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int sticks[]={6,2,5,5,4,5,6,3,7,6}; 4 | int getSum(int n) 5 | { 6 | int sum = 0; 7 | while (n != 0) 8 | { 9 | sum+=sticks[(n%10)]; 10 | n = n/10; 11 | } 12 | return sum; 13 | } 14 | int main() 15 | { 16 | int T; 17 | int A,B; 18 | cin>>T; 19 | while(T--) 20 | { 21 | cin>>A>>B; 22 | int sum=A+B; 23 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | int t; 7 | cin>>t; 8 | for(int i=0;i>n>>k; 11 | long long int* a1 = new long long int[n]; 12 | long long int* a2 = new long long int[n]; 13 | for(int j=0;j>a1[j]; 15 | } 16 | for(int j=0;j>a2[j]; 18 | } 19 | if(k>0){ 20 | int max=0; 21 | for(int j=1;jabs(a2[max])) max=j; 23 | } 24 | if(a2[max]==0) {} 25 | else if(a2[max]>0){ 26 | a1[max]+=k; 27 | } 28 | else { 29 | a1[max]-=k; 30 | } 31 | } 32 | long long int sum=0; 33 | for(int j=0;j 2 | #define sz(a) int((a).size()) 3 | #define pb push_back 4 | #define F first 5 | #define S second 6 | #define all(c) (c).begin(),(c).end() 7 | #define tr(c,i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++) 8 | #define present(c,x) ((c).find(x) != (c).end()) 9 | #define cpresent(c,x) (find(all(c),x) != (c).end()) 10 | using namespace std; 11 | 12 | typedef vector vi; 13 | typedef vector vvi; 14 | typedef pair ii; 15 | typedef vector vii; 16 | typedef vector vvii; 17 | 18 | int main(){ 19 | int n,m; 20 | cin>>n>>m; 21 | vector count(n+1,1); 22 | vector v(n+1); 23 | for(int i=1;i<=n;i++) cin>>v[i]; 24 | for(int i=2;i<=n;i++){ 25 | if(v[i]==v[i-1]) count[i] = count[i-1] + 1; 26 | } 27 | while(m--){ 28 | int l,r,k; 29 | cin>>l>>r>>k; 30 | int mid = (r+l)/2; 31 | int x = count[mid]; 32 | int y = k-x; 33 | if(mid+y>r) cout<<-1< 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | int n; 9 | cin>>n; 10 | vector v(n); 11 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | void print(int a[], int n){ 6 | for(int i=0;i>t; 22 | for(int i=0;i>n>>k; 25 | long long int* a = new long long int[n]; 26 | for(int j=0;j>a[j]; 27 | sort(a,a+n); 28 | long long int p=1; 29 | long long int p2=1; 30 | int li=0; 31 | int ri=n-1; 32 | if(k%2==1) { 33 | p=modu(a[n-1]); 34 | k--; 35 | ri--; 36 | } 37 | //print(a,n); 38 | while(k){ 39 | long long int pro1=(a[li]*a[li+1]); 40 | long long int pro2=(a[ri]*a[ri-1]); 41 | // cout<pro1){ 43 | pro2 = modu(pro2); 44 | p2=modu(p2*pro2); 45 | ri=ri-2; 46 | if (pro1<0) li++; 47 | } 48 | else{ 49 | pro1 = modu(pro1); 50 | p2=modu(p2*pro1); 51 | li=li+2; 52 | } 53 | k=k-2; 54 | } 55 | p=modu(p*p2); 56 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | long long int n; 9 | cin>>n; 10 | long long int min=100000; 11 | for(int i=0;i>temp; 14 | if(temp 2 | using namespace std; 3 | 4 | typedef long long LL; 5 | typedef long double LD; 6 | typedef pair pii; 7 | typedef pair pll; 8 | 9 | int main() { 10 | #ifndef ONLINE_JUDGE 11 | freopen("inp.txt", "r", stdin); 12 | #endif 13 | int t, n, ans; 14 | scanf("%d", &t); 15 | while(t--) { 16 | scanf("%d", &n); 17 | vector a(2*n); 18 | for(int i = 0; i < 2*n; ++i) { 19 | scanf("%d", &a[i]); 20 | } 21 | sort(a.begin(), a.end()); 22 | ans = a[n+n/2]; 23 | printf("%d\n", ans); 24 | for(int i = 0; i < n; ++i) { 25 | printf("%d %d ", a[i], a[i+n]); 26 | } 27 | printf("\n"); 28 | } 29 | return 0; 30 | } -------------------------------------------------------------------------------- /codechef/numgame.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | for(int i=0;i>n; 10 | if(n%2==0) cout<<"ALICE"; 11 | else cout<<"BOB"; 12 | } 13 | } -------------------------------------------------------------------------------- /codechef/p1z2s.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | int n; 7 | cin>>n; 8 | int* a = new int [n]; 9 | int* b = new int [n]; 10 | for (int i=0;i>a[i]; 11 | sort(a,a+n); 12 | long long int sum=0; 13 | int j=0; 14 | if(n==1) cout<< (a[0]/2)+(a[0]%2) << endl; 15 | for (int i=0;i1) { 24 | //cout<0: 7 | n = (n*(n+1))/2 8 | d -= 1 9 | print n -------------------------------------------------------------------------------- /codechef/rainbowa.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | int n; 9 | cin>>n; 10 | vector v(n); 11 | for(int i=0;i>v[i]; 12 | bool ans = true; 13 | vector r; 14 | int c=1; 15 | int i=0; 16 | while(i rr = r; 51 | reverse(r.begin(), r.end()); 52 | if(r!=rr || r.size()!=13) ans=false; 53 | if(ans) cout<<"yes"< 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | int n,m; 9 | cin>>n>>m; 10 | int x = __gcd(max(m,n),min(m,n)); 11 | cout<<(n/x)*(m/x)< 2 | using namespace std; 3 | typedef pair ii; 4 | 5 | 6 | int main(){ 7 | int t; 8 | cin>>t; 9 | while(t--){ 10 | int n,m; 11 | cin>>n>>m; 12 | vector v(n); 13 | for(int i=0;i>s; 16 | v[i] = "A" + s + "A"; 17 | } 18 | bool ans=true; 19 | for(int j=1;j<=m;j++){ 20 | bool air=false; 21 | bool water=false; 22 | for(int i=n-1;i>=0;i--){ 23 | if(v[i][j]=='A') air=true; 24 | if(v[i][j]=='W') water=true; 25 | if(v[i][j]=='B' && (air==true || water==true)){ 26 | ans=false; 27 | break; 28 | } 29 | if(v[i][j]=='W' && (air==true || v[i][j-1]=='A' || v[i][j+1]=='A')){ 30 | ans=false; 31 | break; 32 | } 33 | } 34 | if(!ans) break; 35 | } 36 | if(ans) cout<<"yes"< 2 | using namespace std; 3 | 4 | int main(){ 5 | cout<<"Enter the size of array\n"; 6 | int n; 7 | cin>>n; 8 | int* a= new int[n]; 9 | cout<<"Enter element one by one\n"; 10 | for(int i=0;i>a[i]; 11 | 12 | return 0; 13 | } 14 | 15 | 16 | 12 123 1234 12 121 4 17 | 112 3 18 | 211 3 19 | 121 4 20 | 4123 21 | 312 3124 22 | 4312 23 | 21 213 2134 24 | 4213 25 | 321 3214 26 | 4321 -------------------------------------------------------------------------------- /codechef/samesnak.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | int* a = new int[4]; 9 | int* b = new int[4]; 10 | for(int i=0;i<4;i++) cin>>a[i]; 11 | for(int i=0;i<4;i++) cin>>b[i]; 12 | bool ans = false; 13 | if(a[0]==a[2]){ // vertical 14 | if(a[1]>a[3]) swap(a[1],a[3]); 15 | if(b[1]>b[3]) swap(b[1],b[3]); 16 | if( (a[1] < b[1] && a[3]>b[1]) || (b[1]a[1]) ){ 17 | ans = true; 18 | // cout<<"yyy"<a[2]) swap(a[0],a[2]); 32 | if(b[0]>b[2]) swap(b[0],b[2]); 33 | if( (a[0] < b[0] && a[2]>b[0]) || (b[0]a[0]) ){ 34 | ans = true; 35 | // cout<<"yyy"< 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | int l; 9 | cin>>l; 10 | string s; 11 | cin>>s; 12 | char expected = 'H'; 13 | bool ans=true; 14 | for(int i=0;i 2 | #define SIZE 1001 3 | using namespace std; 4 | 5 | int main(){ 6 | int t; 7 | cin>>t; 8 | for(int i=0;i>s1; 13 | cin>>s2; 14 | bool flag=false; 15 | for(int j=0 ; s1[j]!='\0';j++){ 16 | for(int z=0; s2[z]!= '\0';z++){ 17 | if(s1[j]==s2[z]) { 18 | cout<<"Yes"< 2 | int main() 3 | { 4 | int t; 5 | scanf("%d",&t); 6 | while(t--) 7 | { 8 | int n; 9 | scanf("%d",&n); 10 | int a[n][n]; 11 | for(int i=0;i=0;i--) 15 | for(int j=0;j<=i;j++) 16 | a[i][j]+=((a[i+1][j]>a[i+1][j+1])?a[i+1][j]:a[i+1][j+1]); 17 | printf("%d\n",a[0][0]); 18 | } 19 | return 0; 20 | } -------------------------------------------------------------------------------- /codechef/templela.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | int s,j,t; 6 | scanf("%d",&t); 7 | while(t--) 8 | { 9 | 10 | int n,b,i,a=0; 11 | scanf("%d",&b); 12 | int h[b]; 13 | for(i=0;i 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | int l; 9 | cin>>l; 10 | int* a = new int[l]; 11 | for(int i=0;i>a[i]; 12 | bool ans=true; 13 | if(l%2==0) ans=false; 14 | else{ 15 | int e=1; 16 | for(int i=0;i 2 | using namespace std; 3 | typedef pair ii; 4 | 5 | void tour(int s, vector > &g, vector > &h, vector &e){ 6 | while(!g[s].empty()){ 7 | ii t = *(g[s].begin()); 8 | g[s].erase(t); 9 | h[t.first].erase(ii(s,t.second)); 10 | tour(t.first,g,h,e); 11 | } 12 | while(!h[s].empty()){ 13 | ii t = *(h[s].begin()); 14 | h[s].erase(t); 15 | g[t.first].erase(ii(s,t.second)); 16 | swap(e[t.second].first,e[t.second].second); 17 | tour(t.first,g,h,e); 18 | } 19 | } 20 | 21 | int main(){ 22 | int n,e; 23 | cin>>n>>e; 24 | vector > g(n+1); 25 | vector > h(n+1); 26 | vector ed; 27 | for(int i=0;i>a>>b; 30 | ed.push_back(ii(a,b)); 31 | g[a].insert(ii(b,i)); 32 | h[b].insert(ii(a,i)); 33 | } 34 | bool ans=true; 35 | for(int i=1;i<=n;i++){ 36 | int x = g[i].size(); 37 | int y = h[i].size(); 38 | if((x+y)%2 || x+y==0) {ans=false; break;} 39 | } 40 | if(ans==false) { 41 | cout<<"NO"< 2 | #include 3 | using namespace std; 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | for(int i=0;i>b; 10 | int n = b/2; 11 | n=n-1; 12 | if(n<0) cout<<0< 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | string a,b; 9 | cin>>a>>b; 10 | if(a.size()!=b.size()) { 11 | cout<<"No"< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | int t; 7 | cin>>t; 8 | while(t--){ 9 | int n; 10 | cin>>n; 11 | if(n==1){ 12 | cout<<1<<"\n"; 13 | cout<<1<<" "<<1<<"\n"; 14 | } 15 | else{ 16 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int i,j,k; 6 | int n1,n2,n3,n=0; 7 | set s; 8 | set a; 9 | cin>>n1>>n2>>n3; 10 | for(i=0;i>j; 13 | s.insert(j); 14 | } 15 | for(i=0;i>j; 18 | if(s.find(j)==s.end()) 19 | s.insert(j); 20 | else 21 | a.insert(j); 22 | } 23 | for(i=0;i>j; 26 | if(s.find(j)==s.end()) 27 | s.insert(j); 28 | else 29 | a.insert(j); 30 | } 31 | cout<::iterator it=a.begin();it!=a.end();it++) 33 | { 34 | cout<<*it< 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int n,h; 8 | cin>>n>>h; 9 | int stack[n]; 10 | for(int i=0;i>stack[i]; 12 | int com,crane=0,loaded=0; 13 | do 14 | { 15 | cin>>com; 16 | switch(com) 17 | { 18 | case 1: if(crane != 0) crane--;break; 19 | case 2: if(crane != (n-1)) crane++;break; 20 | case 3: if(loaded == 0 && stack[crane] != 0){stack[crane]--; loaded = 1;}break; 21 | case 4: if(loaded == 1 && stack[crane] != (h)){stack[crane]++; loaded = 0;}break; 22 | } 23 | }while(com!=0); 24 | for(int i=0; i 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | unsigned long long int s, a, b, c; 7 | 8 | cin >> t; 9 | 10 | for (int i = 0; i < t; i++) { 11 | cin >> s >> a >> b >> c; 12 | unsigned long long int numFree = b * (s / (a * c)); 13 | unsigned long long int numBuy = ((s / (a * c))) * a; 14 | unsigned long long int remMon = s - (numBuy * c); 15 | unsigned long long int remBuy = remMon / c; 16 | unsigned long long int total = numBuy + numFree + remBuy; 17 | cout << total << endl; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /codeforces/A/118a.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | string s; 6 | cin>>s; 7 | string ans=""; 8 | for(int i=0;i='A' && s[i]<='Z') { 15 | c = s[i]-'A'+'a'; 16 | } 17 | ans.push_back('.'); 18 | ans.push_back(c); 19 | } 20 | } 21 | cout< 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int t; 7 | cin >> t; 8 | 9 | int a,b,n, ans; 10 | while(t--) { 11 | cin >> a >> b >> n; 12 | 13 | if (n % 3 == 0) { 14 | ans = a; 15 | } else if (n % 3 == 1) { 16 | ans = b; 17 | } else { 18 | ans = a ^ b; 19 | } 20 | 21 | cout << ans << "\n"; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /codeforces/A/158a.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n,k; 6 | cin>>n>>k; 7 | vector v(n); 8 | for(int i=0;i>v[i]; 10 | } 11 | while(v[k]==v[k-1] && k0 && v[k-1]<=0) k--; 13 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | long long int n,m,a; 6 | cin>>n>>m>>a; 7 | cout<< ((m+a-1)/a) * ((n+a-1)/a)< 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | char a[100]; 9 | int b[26]; 10 | int flag=0; 11 | int i,j,c=0; 12 | for (i = 0; i < 26; ++i) 13 | { 14 | b[i]=0; 15 | } 16 | scanf("%s",a); 17 | for (i = 0; i < strlen(a);i++) 18 | { 19 | j=a[i]; 20 | j=j-97; 21 | b[j]++; 22 | } 23 | for(i=0;i<26;i++) 24 | { 25 | if(b[i]>0) 26 | c++; 27 | } 28 | if(c%2==0) 29 | cout<<"CHAT WITH HER!"< 2 | using namespace std; 3 | 4 | int main(){ 5 | int w; 6 | cin>>w; 7 | if(w%2 || w==2) cout<<"NO"< 2 | using namespace std; 3 | 4 | int main(){ 5 | int m,n; 6 | cin>>m>>n; 7 | cout<< (m*n)/2 < 2 | using namespace std; 3 | int main() 4 | { 5 | int n; 6 | cin>>n; 7 | char a[n]; 8 | char b[n]; 9 | int i,j,k; 10 | int c=0; 11 | int d=0; 12 | for(i=0;i>a[i]; 14 | for(i=0;i>b[i]; 16 | for(i=0;i5) 20 | { 21 | if(b[i]>a[i]) 22 | d=(a[i]+10)-b[i]; 23 | else 24 | d=(b[i]+10)-a[i]; 25 | } 26 | c+=d; 27 | } 28 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long long int ll; 6 | ll maxi(int a,int b) 7 | { 8 | if(a>b) 9 | return a; 10 | return b; 11 | } 12 | ll f(ll A,ll B){ 13 | if(A==1 && B==1) 14 | return 0; 15 | int c=0; 16 | while(A>0 && B>0) 17 | { 18 | if(A>=B){ 19 | A=A-2; 20 | B++; 21 | } 22 | else{ 23 | A++; 24 | B=B-2; 25 | } 26 | c++; 27 | } 28 | return c; 29 | } 30 | int main() 31 | { 32 | int A,B; 33 | cin>>A>>B; 34 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int n; 6 | cin>>n; 7 | while(n--){ 8 | string s; 9 | cin>>s; 10 | if(s.size()<11) cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int k,r; 6 | cin>>k>>r; 7 | int temp=k; 8 | int count=1; 9 | while(temp%10 != 0 && temp%10 != r){ 10 | count++; 11 | temp+=k; 12 | } 13 | cout< 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin >> n; 8 | int p; 9 | vector a(101,0); 10 | set s; 11 | for (int i = 0; i < n; ++i) 12 | { 13 | cin >> p; 14 | a[p]++; 15 | s.insert(p); 16 | } 17 | 18 | vector sv(s.begin(), s.end()); 19 | 20 | if (sv.size() == 2 && a[sv[0]] == a[sv[1]]) { 21 | cout << "YES" << "\n"; 22 | cout << sv[0] << " " << sv[1] << "\n"; 23 | } else { 24 | cout << "NO" << "\n"; 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /codeforces/B/1042b.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define qx 100000001 4 | using namespace std; 5 | int main() 6 | { 7 | ios_base::sync_with_stdio(false); //optimizes iostream!!! 8 | cin.tie(0); 9 | 10 | int n,i; 11 | long int ar[1000]; 12 | string pc[1000]; 13 | cin>>n; 14 | for(i=0;i>ar[i]>>pc[i]; 17 | } 18 | long int a=qx,b=qx,c=qx,ab=qx,ac=qx,bc=qx,abc=qx; 19 | bool flags[]={0,0,0,0,0,0,0}; 20 | for(i=0;isumset[i]) 52 | minsum=sumset[i]; 53 | if(minsum 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | ios_base::sync_with_stdio(false); 7 | cin.tie(NULL); 8 | long long int a,b,i; 9 | cin>>a>>b; 10 | /*if((b-a)%2==0) 11 | {cout<<"NO"; 12 | return 0; 13 | }*/ 14 | if((b-a)%2!=0) 15 | cout<<"YES"; 16 | for(i=0;i 2 | typedef long long int ll; 3 | using namespace std; 4 | ll f(int n){ 5 | return (1LL*n*(n-1)/2); 6 | } 7 | int __min(int a,int b){ 8 | if(a>b) 9 | return a; 10 | return b; 11 | } 12 | int main(){ 13 | int N,t; 14 | ll ans=0; 15 | cin>>N; 16 | vector v(22,0);//-10 to +10 : 17 | while(N--){ 18 | cin>>t; 19 | if(t>0) 20 | v[t+10]++; 21 | else if(t==0) 22 | v[0]++; 23 | else 24 | v[-t]++; 25 | } 26 | //traverse the hash array 27 | t=v[0]; 28 | ans = f(t); 29 | for(int i=1;i<=10;i++){ 30 | ans+= f(__min(v[i],v[i+10])); 31 | } 32 | cout< 2 | #define fr(a,b,c) for(int a = b, _ = c; a < _; a++) 3 | typedef unsigned long long ui64; 4 | using namespace std; 5 | int v[1100]; 6 | bool dp[1100][1100]; 7 | int main() 8 | { 9 | int n, m; 10 | cin >> n >> m; 11 | if(n > m){ 12 | cout << "YES" << endl; 13 | return 0; 14 | } 15 | fr(i,0,n){ 16 | cin >> v[i]; 17 | v[i] = v[i] % m; 18 | } 19 | dp[0][v[0]] = true; 20 | fr(i, 1, n){ 21 | dp[i][v[i]] = true; 22 | fr(j,0,m){ 23 | if(dp[i-1][j]){ 24 | dp[i][ (j+v[i])%m ] = true; 25 | dp[i][j] = true; 26 | } 27 | } 28 | } 29 | if(dp[n-1][0]){ 30 | cout << "YES" << endl; 31 | }else{ 32 | cout << "NO" << endl; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /codeforces/B/624b.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | vector a; 8 | int main(){ 9 | //freopen("inp.i","r",stdin); 10 | long long int n; 11 | a.clear(); 12 | scanf("%lld",&n); 13 | for(int i=0;i=0;i--) 23 | { 24 | if(temp==0) break; 25 | if(temp>a[i]){ 26 | temp=a[i]; 27 | ans+=temp; continue; 28 | } 29 | if(temp==a[i]){ 30 | temp--; 31 | ans+=temp; continue; 32 | } 33 | if(temp 2 | #include 3 | using namespace std; 4 | 5 | typedef long long big; 6 | 7 | int main() { 8 | int n; 9 | vector cubes; 10 | 11 | cin >> n; 12 | for (int i = 0; i < n; ++i) { 13 | big cube; 14 | cin >> cube; 15 | cubes.push_back(cube); 16 | } 17 | 18 | for (int i = 0; i < n / 2; ++i) { 19 | if (i % 2 == 0) { 20 | big temp = cubes[i]; 21 | cubes[i] = cubes[cubes.size() - 1 - i]; 22 | cubes[cubes.size() - 1 - i] = temp; 23 | } 24 | } 25 | 26 | for (int k = 0; k < cubes.size(); ++k) { 27 | cout << cubes[k]; 28 | if (k != cubes.size() - 1) { 29 | cout << " "; 30 | } 31 | } 32 | cout << "\n"; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /codeforces/B/869b.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | long int a,b; 7 | int result; 8 | cin>>a>>b; 9 | if(a==b) result=1; 10 | else { 11 | result=(a+1)%10; 12 | for(long int i=a+2;i<=b;i++){ 13 | result=(result*(i%10))%10; 14 | if (result==0) break; 15 | } 16 | } 17 | cout< 2 | using namespace std; 3 | map > mp; 4 | vector< bool> b(10000,0); 5 | 6 | int main(){ 7 | long int n,i,num,cou,j; 8 | cin>>n; 9 | 10 | for(i=2;i<=n;i++){ 11 | cin>>num; 12 | mp[num].push_back(i); 13 | } 14 | 15 | for(i=1;i<=n;i++){ 16 | if(mp[i].size()) 17 | b[i]=1; 18 | } 19 | 20 | } 21 | 22 | for(i=1;i<=n;i++){ 23 | cou=0; 24 | if(b[i]){ 25 | for(j=0;j 2 | 3 | using namespace std; 4 | int n; 5 | char s[200005]; 6 | stack stk; 7 | 8 | int main() { 9 | cin >> n; 10 | 11 | for (int i=0;i> s[i]; 13 | if (s[i] == '(') { 14 | stk.push(s[i]); 15 | } else { 16 | if (!stk.empty() && stk.top() == '(') { 17 | stk.pop(); 18 | } else { 19 | stk.push(s[i]); 20 | } 21 | } 22 | } 23 | 24 | if (stk.empty()) { 25 | cout << "Yes\n"; 26 | } else if (stk.size() == 2) { 27 | char first = stk.top(); 28 | stk.pop(); 29 | char last = stk.top(); 30 | cout << (first != last ? "Yes" : "No"); 31 | } else { 32 | cout << "No\n"; 33 | } 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /codeforces/C/1426C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define deb(x) cout << #x << ": " << x << endl 3 | #define ll long long 4 | using namespace std; 5 | 6 | int main(){ 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(NULL); 9 | int t; 10 | cin>>t; 11 | 12 | while(t--){ 13 | int n; 14 | cin>>n; 15 | int ans = 1e9; 16 | 17 | for(int x=1;x*x<=n;x++){ 18 | ans = min(ans, x-1+(n-1)/x); 19 | } 20 | cout< 2 | #include 3 | using namespace std; 4 | long long n,a[500009],sum[500009],ans=0; 5 | int main() 6 | { 7 | scanf("%lld",&n); 8 | memset(a,0,sizeof a); 9 | memset(sum,0,sizeof sum); 10 | for(int i=1;i<=n;i++) 11 | { 12 | scanf("%lld",&a[i]); 13 | sum[i]=sum[i-1]+a[i]; 14 | } 15 | long long num=0; 16 | if(sum[n]%3!=0) 17 | { 18 | printf("0"); return 0; 19 | } 20 | if(sum[n]==0) 21 | { 22 | for(int i=1;i 2 | using namespace std; 3 | int main(){ 4 | map m; 5 | int N; 6 | string s; 7 | cin>>N; 8 | while(N--){ 9 | cin>>s; 10 | map::iterator itr = m.find(s); 11 | if(itr == m.end()){ 12 | m[s]=1; 13 | cout<<"OK"< 2 | using namespace std; 3 | int main(){ 4 | map m; 5 | int N; 6 | string s; 7 | cin>>N; 8 | while(N--){ 9 | cin>>s; 10 | map::iterator itr = m.find(s); 11 | if(itr == m.end()){ 12 | m[s]=1; 13 | cout<<"OK"< 2 | typedef long long int ll; 3 | using namespace std; 4 | int main(){ 5 | int E,N; 6 | cin>>E>>N; 7 | ll count=0; 8 | if(E==1 && N==1){ 9 | cout<<"0"<0 && N>0){ 13 | if(E>=N){ 14 | E-=2;N--; 15 | } 16 | else{ 17 | E--;N-=2; 18 | } 19 | count++; 20 | if(E==1 && N==1) 21 | break; 22 | } 23 | cout< 2 | typedef long long int ll; 3 | using namespace std; 4 | int main(){ 5 | int E,N; 6 | cin>>E>>N; 7 | ll count=0; 8 | if(E==1 && N==1){ 9 | cout<<"0"<0 && N>0){ 13 | if(E>=N){ 14 | E-=2;N--; 15 | } 16 | else{ 17 | E--;N-=2; 18 | } 19 | count++; 20 | if(E==1 && N==1) 21 | break; 22 | } 23 | cout< 2 | using namespace std; 3 | int main(){ 4 | int N,k1,k2,t; 5 | list A,B; 6 | map,list > m; 7 | cin>>N; 8 | cin>>k1; 9 | for(int i=0;i>t; 11 | A.push_back(t); 12 | } 13 | cin>>k2; 14 | for(int i=0;i>t; 16 | B.push_back(t); 17 | } 18 | int a,b,c=0; 19 | while(A.size()!=0 && B.size()!=0) 20 | { 21 | a=A.front();A.pop_front(); 22 | b=B.front();B.pop_front(); 23 | if(a>b){ 24 | A.push_back(b);A.push_back(a); 25 | } 26 | else{ 27 | B.push_back(a);B.push_back(b); 28 | } 29 | if(m.find(A)!=m.end() && m[A]==B){ 30 | cout<<"-1"< 2 | using namespace std; 3 | int main(){ 4 | int N,k1,k2,t; 5 | list A,B; 6 | map,list > m; 7 | cin>>N; 8 | cin>>k1; 9 | for(int i=0;i>t; 11 | A.push_back(t); 12 | } 13 | cin>>k2; 14 | for(int i=0;i>t; 16 | B.push_back(t); 17 | } 18 | int a,b,c=0; 19 | while(A.size()!=0 && B.size()!=0) 20 | { 21 | a=A.front();A.pop_front(); 22 | b=B.front();B.pop_front(); 23 | if(a>b){ 24 | A.push_back(b);A.push_back(a); 25 | } 26 | else{ 27 | B.push_back(a);B.push_back(b); 28 | } 29 | if(m.find(A)!=m.end() && m[A]==B){ 30 | cout<<"-1"< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #define MOD 1000000007 16 | typedef long long int ll; 17 | using namespace std; 18 | int main() 19 | { 20 | ios_base::sync_with_stdio(0); 21 | string N; 22 | cin>>N; 23 | //Step 0 24 | int i = N.find('0'); 25 | if(N[i]=='0') 26 | { 27 | cout<<"YES\n0\n"; 28 | return 0; 29 | } 30 | i = N.find('8'); 31 | if(N[i]=='8') 32 | { 33 | cout<<"YES\n8\n"; 34 | return 0; 35 | } 36 | //Step 1 37 | int K=0; 38 | for(i=N.size()-1;i>=0;i--) 39 | { 40 | K = N[i]-'0'; 41 | if(K%2==0) 42 | break; 43 | } 44 | if(K==4) 45 | { 46 | for(i--;i>=0;i--) 47 | { 48 | K = N[i]-'0'; 49 | if(K%2==0) 50 | { 51 | cout<<"YES\n"; 52 | cout<=0;i--) 60 | { 61 | int T = N[i]-'0'; 62 | if(T&1) 63 | { 64 | cout<<"YES\n"; 65 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #define MOD 1000000007 16 | typedef long long int ll; 17 | using namespace std; 18 | int main() 19 | { 20 | ios_base::sync_with_stdio(0); 21 | string N; 22 | cin>>N; 23 | //Step 0 24 | int i = N.find('0'); 25 | if(N[i]=='0') 26 | { 27 | cout<<"YES\n0\n"; 28 | return 0; 29 | } 30 | i = N.find('8'); 31 | if(N[i]=='8') 32 | { 33 | cout<<"YES\n8\n"; 34 | return 0; 35 | } 36 | //Step 1 37 | int K=0; 38 | for(i=N.size()-1;i>=0;i--) 39 | { 40 | K = N[i]-'0'; 41 | if(K%2==0) 42 | break; 43 | } 44 | if(K==4) 45 | { 46 | for(i--;i>=0;i--) 47 | { 48 | K = N[i]-'0'; 49 | if(K%2==0) 50 | { 51 | cout<<"YES\n"; 52 | cout<=0;i--) 60 | { 61 | int T = N[i]-'0'; 62 | if(T&1) 63 | { 64 | cout<<"YES\n"; 65 | cout< 2 | #include 3 | using namespace std; 4 | int N, M; 5 | vector< vector > G; 6 | vector< int > vertexData, visited; 7 | int DFS_modified(int v, int c) 8 | { 9 | visited[v] = 1; 10 | //do stuff 11 | if(vertexData[v] == 1) c++; 12 | else c = 0; 13 | if(c > M) 14 | return 0; 15 | //check for leaf 16 | if(v > 1 && G[v].size() == 1)//it is leaf 17 | { 18 | if(c > M) return 0; 19 | return 1; 20 | } 21 | int ans = 0; 22 | for(int i = 0; i < G[v].size(); i++){ 23 | if(visited[G[v][i]] == 0) 24 | ans += DFS_modified(G[v][i], c); 25 | } 26 | return ans; 27 | } 28 | int main() 29 | { 30 | cin >> N >> M; 31 | vertexData.resize(N + 1); 32 | visited.resize(N + 1, 0); 33 | G.resize(N + 1, vector()); 34 | for(int i = 1; i <= N; i++) 35 | cin >> vertexData[i]; 36 | //edge list 37 | int x, y; 38 | for(int i = 1; i < N; i++) 39 | { 40 | cin >> x >> y; 41 | G[x].push_back(y); 42 | G[y].push_back(x); 43 | } 44 | cout << DFS_modified(1, 0) << endl; 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /codeforces/C/580c.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int N,M; 5 | vector< vector > G; 6 | vector< int > vertexData,visited; 7 | int DFS_modified(int v,int c){ 8 | visited[v]=1; 9 | //do stuff 10 | if(vertexData[v]==1) c++; 11 | else c=0; 12 | if(c>M) 13 | return 0; 14 | //check for leaf 15 | if(v>1 && G[v].size()==1)//it is leaf 16 | { 17 | if(c>M) return 0; 18 | return 1; 19 | } 20 | int ans=0; 21 | for(int i=0;i>N>>M; 29 | vertexData.resize(N+1); 30 | visited.resize(N+1,0); 31 | G.resize(N+1,vector()); 32 | for(int i=1;i<=N;i++) 33 | cin>>vertexData[i]; 34 | //edge list 35 | int x,y; 36 | for(int i=1;i>x>>y; 38 | G[x].push_back(y); 39 | G[y].push_back(x); 40 | } 41 | cout< 2 | #include 3 | #include 4 | #define ll long long 5 | #define INF 0x3f3f3f3f 6 | #define MAXN 10000 7 | #define mod 1000000007 8 | #define pb push_back 9 | #define fast std::ios_base::sync_with_stdio (false); cin.tie(NULL); cout.tie(NULL) 10 | using namespace std; 11 | int z; 12 | 13 | //vectora(10005); 14 | 15 | char fun(char x,char y){ 16 | for(int i=97;i<=122;i++){ 17 | //cout<>s; 25 | 26 | for(int i=1;i 4 | #include 5 | typedef long long int ll; 6 | using namespace std; 7 | int main(){ 8 | double N; 9 | ll n; 10 | cin>>n; 11 | N=n; 12 | double l = log2(N); 13 | //cout< 4 | #include 5 | typedef long long int ll; 6 | using namespace std; 7 | int main(){ 8 | double N; 9 | ll n; 10 | cin>>n; 11 | N=n; 12 | double l = log2(N); 13 | //cout< 2 | #include 3 | #include 4 | using namespace std; 5 | class node{ 6 | public: 7 | int t,k,d; 8 | node(int tt,int kk,int dd){ 9 | t=tt; 10 | k=kk; 11 | d=dd; 12 | } 13 | }; 14 | int main(){ 15 | int N,Q,t,k,d; 16 | cin>>N>>Q; 17 | queue job; 18 | while(Q--){ 19 | cin>>t>>k>>d; 20 | job.push(node(t,k,d)); 21 | } 22 | job.push(node(0,0,0)); 23 | node j = job.front();job.pop(); 24 | vector v(N+1,0); 25 | for(int t=1;;t++){ 26 | //cout<<"ho"<0) 52 | v[i]--; 53 | } 54 | if(j.t==0) 55 | break; 56 | } 57 | } -------------------------------------------------------------------------------- /codeforces/C/747c.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | class node{ 6 | public: 7 | int t,k,d; 8 | node(int tt,int kk,int dd){ 9 | t=tt; 10 | k=kk; 11 | d=dd; 12 | } 13 | }; 14 | int main(){ 15 | int N,Q,t,k,d; 16 | cin>>N>>Q; 17 | queue job; 18 | while(Q--){ 19 | cin>>t>>k>>d; 20 | job.push(node(t,k,d)); 21 | } 22 | job.push(node(0,0,0)); 23 | node j = job.front();job.pop(); 24 | vector v(N+1,0); 25 | for(int t=1;;t++){ 26 | //cout<<"ho"<0) 52 | v[i]--; 53 | } 54 | if(j.t==0) 55 | break; 56 | } 57 | } -------------------------------------------------------------------------------- /codeforces/C/755C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | map > mp; 4 | vector< bool> b(10000,0); 5 | vectorvisited(100000,0); 6 | 7 | void dfs(long int n){ 8 | visited[n]=true; 9 | 10 | for(int i=0;i>n; 19 | 20 | for(i=1;i<=n;i++){ 21 | cin>>num; 22 | mp[i].push_back(num); 23 | mp[num].push_back(i); 24 | } 25 | 26 | for(i=1;i<=n;i++){ 27 | 28 | if(!visited[i]){ 29 | cou++; 30 | dfs(i); 31 | } 32 | } 33 | cout< 2 | 3 | using namespace std; 4 | 5 | #define mp make_pair 6 | #define pb push_back 7 | #define fi first 8 | #define se second 9 | #define f(i,a,b) for (int i = (a); i < (b); i++) 10 | #define fr(i,a,b) for (int i = (a); i >= b; i--) 11 | #define all(a) a.begin(), a.end() 12 | #define MOD 1e9+7 13 | 14 | typedef long long ll; 15 | typedef vector vi; 16 | typedef pair pii; 17 | typedef vector< pii > vii; 18 | 19 | #define MAX 100010 20 | bool visited[MAX]; 21 | vi adj[MAX]; 22 | int cost[MAX]; 23 | 24 | int bfs(int s) { 25 | int min_cost = cost[s]; 26 | visited[s] = true; 27 | queue q; 28 | q.push(s); 29 | while (!q.empty()) { 30 | int now = q.front(); q.pop(); 31 | for (auto c: adj[now]) { 32 | if (!visited[c]) { 33 | q.push(c); min_cost = min(min_cost, cost[c]); 34 | visited[c] = true; 35 | } 36 | } 37 | } 38 | return min_cost; 39 | } 40 | 41 | int main() { 42 | ios::sync_with_stdio(0); 43 | cin.tie(0); 44 | memset(visited, false, sizeof(visited)); 45 | for (int i = 0; i < MAX; i++) adj[i].clear(); 46 | int n, m, x, y; cin >> n >> m; 47 | for (int i = 1; i <= n; i++) cin >> cost[i]; 48 | for (int i = 0; i < m; i++) { 49 | cin >> x >> y; 50 | adj[x].pb(y); adj[y].pb(x); 51 | } 52 | ll ans = 0; 53 | for (int i = 1; i <= n; i++) { 54 | if (!visited[i]) { 55 | ans += bfs(i); 56 | } 57 | } 58 | cout << ans << endl; 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /codeforces/D/1106D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | vector v[100001]; 8 | bool Passed[100001]; 9 | set st; 10 | 11 | int main () { 12 | //freopen("in", "r", stdin); 13 | int n, m; 14 | scanf("%d %d", &n, &m); 15 | 16 | for (int i = 0; i < m; i++) { 17 | int x, y; 18 | scanf("%d %d", &x, &y); 19 | v[x].push_back(y); 20 | v[y].push_back(x); 21 | } 22 | 23 | st.insert(1); 24 | 25 | while (!st.empty()) { 26 | set::iterator ret = st.begin(); 27 | int vec = *st.begin(); 28 | Passed[vec] = true; 29 | printf("%d ", vec); 30 | 31 | for (int i = 0; i < v[vec].size(); i++) { 32 | if (Passed[v[vec][i]] == false) 33 | st.insert(v[vec][i]); 34 | } 35 | 36 | st.erase(ret); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /codeforces/D/246D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define MAX 100001 5 | 6 | int main() { 7 | int n, m; 8 | cin >> n >> m; 9 | int a[n+1]; a[0] = 0; 10 | for (int i = 1; i <= n; i++) cin >> a[i]; 11 | set s[MAX]; 12 | for (int i = 0; i < m; i++) { 13 | int x, y; 14 | cin >> x >> y; 15 | if (a[x] != a[y]) { 16 | s[a[x]].insert(a[y]); 17 | s[a[y]].insert(a[x]); 18 | } 19 | } 20 | int max_val = -MAX; 21 | int color = 1; 22 | sort(a+1, a+n+1); 23 | for (int i = 1; i <= n; i++) { 24 | if ((int)s[a[i]].size() > max_val) { 25 | color = a[i]; max_val = (int)s[a[i]].size(); 26 | } 27 | } 28 | cout << color << endl; 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /spoj/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gopalgoel19/Competitive-Programming-Solutions/571ff5b9b81e980203ff5efae1595ac69ae9891f/spoj/a.exe -------------------------------------------------------------------------------- /spoj/abcdef.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // a*b + c = d * (e + f) 5 | 6 | int main(){ 7 | ios_base::sync_with_stdio(false); 8 | int n; 9 | cin>>n; 10 | vector v(n); 11 | vector v1; 12 | vector v2; 13 | for(int i=0;i>v[i]; 14 | sort(v.begin(),v.end()); 15 | for(int i=0;i 2 | using namespace std; 3 | 4 | int h,w; 5 | 6 | int dfs(vector &v,vector > &p, int i,int j, int l){ 7 | if(i>=h || i<0 || j>=w || j<0) return 0; 8 | if(p[i][j]!=-1 && v[i][j]=='A'+l) return p[i][j]; 9 | if(v[i][j]=='A'+l){ 10 | vector m; 11 | m.push_back(1 + dfs(v,p,i+1,j,l+1)); 12 | m.push_back(1 + dfs(v,p,i+1,j+1,l+1)); 13 | m.push_back(1 + dfs(v,p,i+1,j-1,l+1)); 14 | m.push_back(1 + dfs(v,p,i,j+1,l+1)); 15 | m.push_back(1 + dfs(v,p,i,j-1,l+1)); 16 | m.push_back(1 + dfs(v,p,i-1,j,l+1)); 17 | m.push_back(1 + dfs(v,p,i-1,j+1,l+1)); 18 | m.push_back(1 + dfs(v,p,i-1,j-1,l+1)); 19 | sort(m.begin(),m.end()); 20 | p[i][j]=m[7]; 21 | return m[7]; 22 | } 23 | else { 24 | return 0; 25 | } 26 | } 27 | 28 | int main(){ 29 | ios_base::sync_with_stdio(false); 30 | int z=0; 31 | while(true){ 32 | z++; 33 | cin>>h>>w; 34 | if(h==0) break; 35 | vector v(h); 36 | vector > p(h); 37 | for(int i=0;i>v[i]; 39 | int max=0; 40 | for(int i=0;imax) max=x; 45 | } 46 | } 47 | } 48 | cout<<"Case "< 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | string a,b,c; 9 | char temp; 10 | cin>>a>>temp>>b>>temp>>c; 11 | if(a.find("machula")){ 12 | int t = atoi(c.c_str())-atoi(b.c_str()); 13 | a= to_string(t); 14 | } 15 | else if(b.find("machula")){ 16 | int t = atoi(c.c_str())-atoi(a.c_str()); 17 | b= to_string(t); 18 | } 19 | else if(c.find("machula")){ 20 | int t = atoi(a.c_str())+atoi(b.c_str()); 21 | c= to_string(t); 22 | } 23 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | while(true){ 6 | int a,b,c; 7 | cin>>a>>b>>c; 8 | if(a==0 && b==0 && c==0) break; 9 | if(b-a==c-b) cout<<"AP "< 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | int j=0; 7 | while(++j){ 8 | int n; 9 | scanf("%d",&n); 10 | if(n==0) break; 11 | vector > v(n); 12 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | int t; 7 | cin>>t; 8 | while(t--){ 9 | int n,m; 10 | cin>>n; 11 | vector v(n); 12 | for(int i=0;i>v[i]; 13 | cin>>m; 14 | vector w(m); 15 | for(int i=0;i>w[i]; 16 | sort(v.begin(), v.end()); 17 | sort(w.begin(), w.end()); 18 | int i=0,j=0; 19 | int diff=abs(v[0]-w[0]); 20 | bool temp=v[0]>w[0]; 21 | while(i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | int M, N = 0, pointer = 0; 12 | long long ret = 0; 13 | pair original [50000], rects [50000]; 14 | vector> lines; 15 | 16 | bool bad(int a, int b, int c){ 17 | return (lines[c].second-lines[a].second)*(lines[a].first-lines[b].first) 18 | < (lines[b].second-lines[a].second)*(lines[a].first-lines[c].first); 19 | } 20 | 21 | void add(long long slope, long long intercept){ 22 | lines.push_back(make_pair(slope, intercept)); 23 | while(lines.size() >= 3 && bad(lines.size()-3, lines.size()-2, lines.size()-1)) lines.erase(lines.end()-2); 24 | } 25 | 26 | long long query(long long val){ 27 | if(pointer > lines.size()-1) pointer = lines.size()-1; 28 | while(pointer < lines.size()-1 && lines[pointer+1].first*val+lines[pointer+1].second < lines[pointer].first*val+lines[pointer].second) 29 | pointer++; 30 | return lines[pointer].first*val+lines[pointer].second; 31 | } 32 | 33 | int main(){ 34 | scanf("%d", &M); 35 | for(int i = 0; i < M; i++) scanf("%lld %lld", &original[i].first, &original[i].second); 36 | sort(original, original+M); 37 | for(int i = 0; i < M; i++){ 38 | while(N > 0 && rects[N-1].second <= original[i].second) N--; 39 | rects[N++] = original[i]; 40 | } 41 | add(rects[0].second, 0); 42 | for(int i = 0; i < N; i++){ 43 | ret = query(rects[i].first); 44 | if(i < N-1) add(rects[i+1].second, ret); 45 | } 46 | cout << ret << '\n'; 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /spoj/addrev.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | // your code goes here 9 | int t; 10 | cin>>t; 11 | for(int i=0;i>s1>>s2; 14 | reverse(s1.begin(),s1.end()); 15 | reverse(s2.begin(),s2.end()); 16 | int n1=atoi(s1.c_str()); 17 | int n2=atoi(s2.c_str()); 18 | string ans = to_string(n1+n2); 19 | for(int i=ans.size()-1;i>=0;i--){ 20 | if(ans[i]=='0') ans.pop_back(); 21 | else break; 22 | } 23 | reverse(ans.begin(),ans.end()); 24 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int n; 6 | cin>>n; 7 | int ans=n; 8 | for(int i=2;in)break; 12 | else{ 13 | t=true; 14 | ans++; 15 | } 16 | } 17 | if(!t) break; 18 | } 19 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #define all(v) (v).begin(),(v).end() 18 | using namespace std ; 19 | vectorc; 20 | int n,m; 21 | bool f(int val){ 22 | int x=0; 23 | for(int i=1;;i++){ 24 | if(i==m)return 1; 25 | x=upper_bound(all(c),c[x]+val-1)-c.begin(); 26 | if(x==c.size())return 0; 27 | } 28 | return 0; 29 | } 30 | int main(){ 31 | //freopen("in.txt","r",stdin); 32 | //freopen("out.txt","w",stdout); 33 | int casos; 34 | scanf("%d",&casos); 35 | for(int cases=0;cases 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | int t; 7 | cin>>t; 8 | while(t--){ 9 | int n; 10 | cin>>n; 11 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #define all(v) (v).begin(),(v).end() 23 | #define rall(v) (v).rbegin(),(v).rend() 24 | using namespace std; // H A M L E T 25 | long long toi(string s){istringstream is(s);long long x;is>>x;return x;} 26 | string tos(long long t){stringstream st; st< > primeFact(int N) 29 | { 30 | vector < pair > V; 31 | 32 | for(int i=2; i*i<=N; i++) 33 | { 34 | int a=0; 35 | while(N%i==0) 36 | { 37 | N/=i; 38 | a++; 39 | } 40 | if(a!=0) V.push_back(make_pair(i, a)); 41 | } 42 | if(N>1) V.push_back(make_pair(N, 1)); 43 | 44 | return V; 45 | } 46 | int main(){ 47 | int tc; 48 | cin>>tc; 49 | while(tc--){ 50 | int n; 51 | cin>>n; 52 | vector >v=primeFact(n); 53 | int cont=0; 54 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | int t; 7 | cin>>t; 8 | while(t--){ 9 | int n,k; 10 | cin>>n>>k; 11 | vector v(n); 12 | for(int i=0;i>v[i]; 13 | sort(v.begin(),v.end()); 14 | int i=0,j=k-1; 15 | int min = v[j]-v[i]; 16 | while(j 2 | using namespace std; 3 | 4 | vector v(1000,true); 5 | vector p; 6 | void sieve(){ 7 | v[0]=false; 8 | v[1]=false; 9 | for(int i=2;i<1000;i++){ 10 | if(v[i]==false) continue; 11 | else p.push_back(i); 12 | if(i*i>1001) continue; 13 | for(int j=2*i;j<1001;){ 14 | v[j]=false; 15 | j+=i; 16 | } 17 | } 18 | } 19 | 20 | int main(){ 21 | ios_base::sync_with_stdio(false); 22 | int t; 23 | cin>>t; 24 | sieve(); 25 | vector l; 26 | l.push_back(30); 27 | l.push_back(42); 28 | int num=43; 29 | while(l.size()<1000){ 30 | int count=0; 31 | for(int i=0;i>n; 45 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | int t; 7 | cin>>t; 8 | for(int k=0;k>s1; 11 | s2=s1; 12 | reverse(s1.begin(),s1.end()); 13 | if(s1==s2) cout<<"YES"< 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | while(true){ 7 | int n,m; 8 | cin>>n; 9 | if(n==0) break; 10 | vector a(n); 11 | for(int i=0;i>a[i]; 12 | cin>>m; 13 | vector b(m); 14 | for(int i=0;i>b[i]; 15 | int i=0,j=0; 16 | int sum1=0,sum2=0; 17 | while(ib[j]){ 23 | sum2+=b[j]; 24 | j++; 25 | } 26 | else{ 27 | sum1 = sum2 = max(sum1,sum2) + a[i]; 28 | i++; 29 | j++; 30 | } 31 | } 32 | while(i 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | int num=1; 7 | while(true){ 8 | string s; 9 | cin>>s; 10 | if(s[0]=='-') break; 11 | int c=0; 12 | int o=0; 13 | int n=s.size(); 14 | int i=0; 15 | while(i 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | long long int a,b; 9 | long long sum; 10 | cin>>a>>b>>sum; 11 | long long int n = (sum*2)/(a+b); 12 | cout<0){ 10 | long ans= sc.nextLong(); 11 | while(sc.hasNext()){ 12 | String t = sc.next(); 13 | if(t.equals("=")) { 14 | System.out.println(ans); 15 | break; 16 | } 17 | Long c = sc.nextLong(); 18 | if(t.equals("+")) ans+=c; 19 | if(t.equals("*")) ans*=c; 20 | if(t.equals("-")) ans-=c; 21 | if(t.equals("/")) ans/=c; 22 | } 23 | tt--; 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /spoj/army.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool myComp(int a,int b){ 5 | return a>b; 6 | } 7 | 8 | int main(){ 9 | int t; 10 | cin>>t; 11 | while(t--){ 12 | int n1,n2; 13 | cin>>n1>>n2; 14 | vector v1; 15 | vector v2; 16 | for(int i=0;i>s; 19 | v1.push_back(s); 20 | } 21 | for(int i=0;i>s; 24 | v2.push_back(s); 25 | } 26 | sort(v1.begin(), v1.end(), myComp); 27 | sort(v2.begin(), v2.end(), myComp); 28 | while(!v1.empty() && !v2.empty()){ 29 | int a=v1.back(); 30 | int b=v2.back(); 31 | if(a 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | int n; 7 | cin>>n; 8 | vector v(n); 9 | for(int i=0;i>v[i]; 10 | int k; 11 | cin>>k; 12 | deque q; 13 | for(int i=0;i=k) q.pop_front(); 22 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | map m; 6 | int n=1; 7 | m[n]=1; 8 | int i=1; 9 | while(n<=1000000000){ 10 | n+= i*6; 11 | m[n]=i+1; 12 | i++; 13 | } 14 | while(true){ 15 | int t; 16 | cin>>t; 17 | if(t==-1) break; 18 | if(m.find(t)!=m.end()) cout<<"Y"< 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | 6 | int main() 7 | { 8 | int tt; 9 | scanf("%d", &tt); 10 | for(int qq = 1; qq <= tt; qq++){ 11 | ll n,m; 12 | scanf("%lld %lld", &n, &m); 13 | if(((n-m)&((m-1)/2)) == 0)printf("1\n"); 14 | else printf("0\n"); 15 | } 16 | return 0; 17 | } -------------------------------------------------------------------------------- /spoj/bishops.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.math.BigInteger; 4 | 5 | public class bishops{ 6 | public static void main(String args[]) throws IOException{ 7 | Scanner sc = new Scanner(System.in); 8 | while(sc.hasNextBigInteger()){ 9 | BigInteger b = sc.nextBigInteger(); 10 | BigInteger t = new BigInteger("1"); 11 | if(b.equals(t)) { 12 | System.out.println("1"); 13 | continue; 14 | } 15 | b=b.multiply(new BigInteger("2")); 16 | b=b.subtract(new BigInteger("2")); 17 | String ans = b.toString(); 18 | System.out.println(ans); 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /spoj/bitmap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | int t; 7 | cin>>t; 8 | while(t--){ 9 | int n,m; 10 | cin>>n>>m; 11 | vector > v(n); 12 | for(int i=0;i>s; 16 | for(int j=0;j > q; 21 | for(int i=0;i p = q.front(); 32 | q.pop(); 33 | int i=p.first; int j=p.second; 34 | if(i+1v[i][j]+1){ 36 | v[i+1][j]=v[i][j]+1; 37 | q.push(make_pair(i+1,j)); 38 | } 39 | } 40 | if(i-1>-1){ 41 | if(v[i-1][j]>v[i][j]+1){ 42 | v[i-1][j]=v[i][j]+1; 43 | q.push(make_pair(i-1,j)); 44 | } 45 | } 46 | if(j+1v[i][j]+1){ 48 | v[i][j+1]=v[i][j]+1; 49 | q.push(make_pair(i,j+1)); 50 | } 51 | } 52 | if(j-1>-1){ 53 | if(v[i][j-1]>v[i][j]+1){ 54 | v[i][j-1]=v[i][j]+1; 55 | q.push(make_pair(i,j-1)); 56 | } 57 | } 58 | } 59 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #define grundy(a,b) memo[a][b] 11 | using namespace std; 12 | 13 | int main(){ 14 | 15 | 16 | int memo[101][101]; 17 | for(int i=0;i<=100;i++)memo[i][0]=memo[0][i]=0; 18 | 19 | 20 | for(int a=1;a<=100;a++){ 21 | for(int b=1;b<=100;b++){ 22 | 23 | bool visited[a*b+3]; 24 | int limit=a*b+2; 25 | memset(visited,0,sizeof(visited)); 26 | 27 | for(int i=0;i>tc; 49 | 50 | while(tc--){ 51 | 52 | int n,m; 53 | scanf("%d %d",&n,&m); 54 | 55 | int g=memo[n][m]; 56 | if(g==0)puts("2"); 57 | else puts("1"); 58 | } 59 | 60 | return 0; 61 | } 62 | 63 | 64 | -------------------------------------------------------------------------------- /spoj/buglife.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | enum{unvisited,visited}; 5 | enum{red,blue}; 6 | 7 | bool checkBipartite(vector > &v, vector &state ,vector &color, int i){ 8 | bool ans=true; 9 | for(int j=0;j>t; 26 | for(int j=1;j<=t;j++){ 27 | int n,m; 28 | cin>>n>>m; 29 | vector > v(n+1); 30 | for(int i=0;i>t1>>t2; 33 | v[t1].push_back(t2); 34 | v[t2].push_back(t1); 35 | } 36 | vector state(n+1,unvisited); 37 | vector color(n+1); 38 | bool ans=true; 39 | for(int i=1;i 2 | using namespace std; 3 | 4 | bool mycomp(pair &p1, pair &p2){ 5 | if(p1.secondp2.second) return false; 7 | else { 8 | return p1.first>t; 16 | for(int k=0;k>n; 19 | vector > v(n); 20 | for(int i=0;i>v[i].first>>v[i].second; 22 | } 23 | sort(v.begin(), v.end(), mycomp); 24 | int count=1; 25 | int end=v[0].second; 26 | for(int i=1;i=end){ 28 | count++; 29 | end=v[i].second; 30 | } 31 | } 32 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | while(true){ 7 | int a[8]; 8 | int b[8]; 9 | for(int i=0;i<8;i++) cin>>a[i]; 10 | if(a[0]==-1) break; 11 | int max=0; 12 | for(int i=0;i<4;i++){ 13 | b[i]=a[i]/a[i+4]; 14 | a[i]=a[i]%a[i+4]; 15 | if(b[i]>=max) { 16 | max=b[i]; 17 | if(a[i]) max++; 18 | } 19 | } 20 | for(int i=0;i<4;i++){ 21 | cout<<((max-b[i])*a[i+4])-a[i]<<" "; 22 | } 23 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | int t; 7 | cin>>t; 8 | for(int k=0;k>n; 11 | vector v1(n); 12 | vector v2(n); 13 | for(int i=0;i>v1[i]>>v2[i]; 14 | sort(v1.begin(), v1.end()); 15 | sort(v2.begin(), v2.end()); 16 | int c=0; 17 | int max=0; 18 | int i=0,j=0; 19 | while(imax) max=c; 24 | } 25 | else{ 26 | c--; 27 | j++; 28 | } 29 | } 30 | cout< 2 | using namespace std; 3 | 4 | int maxi(int a,int b,int c){ 5 | if(a>b){ 6 | if(a>c) return a; 7 | else return c; 8 | } 9 | if(b>c) return b; 10 | else return c; 11 | } 12 | 13 | int main(){ 14 | int t; 15 | cin>>t; 16 | while(t--){ 17 | int r,c; 18 | cin>>r>>c; 19 | if(c==1){ 20 | int ans=0; 21 | for(int i=0;i>tt; 24 | ans+=tt; 25 | } 26 | cout< > v(r); 30 | v[0].resize(c); 31 | for(int j=0;j>v[0][j]; 33 | } 34 | for(int i=1;i>v[i][j]; 38 | if(j==0) v[i][j] += max(v[i-1][j],v[i-1][j+1]); 39 | else if(j==c-1) v[i][j] += max(v[i-1][j],v[i-1][j-1]); 40 | else v[i][j] += maxi(v[i-1][j],v[i-1][j+1],v[i-1][j-1]); 41 | } 42 | } 43 | int ans=v[r-1][0]; 44 | for(int j=0;jans) ans=v[r-1][j]; 46 | } 47 | cout< 2 | using namespace std; 3 | 4 | bool mycomp(int a,int b){ 5 | return a>b; 6 | } 7 | 8 | int main(){ 9 | ios_base::sync_with_stdio(false); 10 | while(true){ 11 | int n; 12 | cin>>n; 13 | if(n==0) break; 14 | vector students(n); 15 | vector price(n); 16 | for(int k=0;k>students[k]; 17 | for(int i=0;i>price[i]; 18 | sort(students.begin(),students.end()); 19 | sort(price.begin(), price.end(),mycomp); 20 | long long int p=0; 21 | for(int i=0;i 2 | using namespace std; 3 | 4 | #define INF (int)1e9 5 | #define EPS 1e-9 6 | #define REP(i, a, b) for(int i = a; i < b; ++i) 7 | #define PB push_back 8 | #define MP make_pair 9 | 10 | typedef long long ll; 11 | typedef pair PII; 12 | typedef vector VI; 13 | typedef vector VS; 14 | typedef vector VII; 15 | 16 | const float PI = 3.1415926535897932384626433832795; 17 | const int MOD = 1000000007; 18 | 19 | int count = 0; 20 | VI G[100010]; 21 | int visited[100010]; 22 | 23 | int bfs(int x) 24 | { 25 | queue q; 26 | int ans = 0; 27 | if(visited[x])return 0; 28 | visited[x] = 1; 29 | q.push(x); 30 | while(!q.empty()){ 31 | int s = q.front(); q.pop(); 32 | ans++; 33 | for (int j = 0; j < G[s].size(); j++) { 34 | int u = G[s][j]; 35 | if (visited[u]) continue; 36 | visited[u] = true; 37 | q.push(u); 38 | } 39 | } 40 | if(ans == 1)return 0; 41 | return ans; 42 | } 43 | 44 | int main() 45 | { 46 | scanf("%d", &tt); 47 | REP(qq, 0, tt){ 48 | int n, e; 49 | scanf("%d %d", &n, &e); 50 | int inf = 0; 51 | int count = 0; 52 | for(int i = 0; i <= n; i++){G[i].clear(); visited[i] = 0;} 53 | for(int i = 0; i < e; i++){ 54 | int x, y; 55 | scanf("%d %d", &x, &y); 56 | G[x].PB(y); G[y].PB(x); 57 | } 58 | if(e == 0)printf("%d\n", n); 59 | else{ 60 | for(int i = 0; i < n; i++){ 61 | int k = bfs(i); 62 | if(k > 0)count++; 63 | inf += k; 64 | } 65 | printf("%d\n", count + (n-inf)); 66 | } 67 | } 68 | return 0; 69 | } -------------------------------------------------------------------------------- /spoj/candy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | while(true){ 6 | int n; 7 | cin>>n; 8 | if(n==-1) break; 9 | vector v; 10 | int sum=0; 11 | for(int i=0;i>x; 14 | sum+=x; 15 | v.push_back(x); 16 | } 17 | if(sum%n!=0) cout<<-1< 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | long long int n; 9 | cin>>n; 10 | long long int sum=0; 11 | for(long long int i=0;i>x; 14 | sum+=x; 15 | sum%=n; 16 | } 17 | if(sum==0) cout<<"YES"< 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | int t; 7 | cin>>t; 8 | while(t--){ 9 | int p; 10 | cin>>p; 11 | int n=0; 12 | int sum=0; 13 | while(sum 2 | using namespace std; 3 | 4 | map m; 5 | 6 | long long int f(int n){ 7 | if(n<12) return n; 8 | else { 9 | long long int a,b,c; 10 | if(m.find(n/2)==m.end()) m[n/2] = f(n/2); 11 | a=m[n/2]; 12 | if(m.find(n/3)==m.end()) m[n/3] = f(n/3); 13 | b=m[n/3]; 14 | if(m.find(n/4)==m.end()) m[n/4] = f(n/4); 15 | c=m[n/4]; 16 | return (long long)(a+b+c); 17 | } 18 | } 19 | 20 | int main(){ 21 | int n; 22 | while(scanf("%d",&n)==1) printf("%lld\n",f(n)); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /spoj/comdiv.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int gcd(int a,int b){ 5 | if(b==0) return a; 6 | else return gcd(b,a%b); 7 | } 8 | 9 | int div(int n){ 10 | int c=0; 11 | int i=0; 12 | for(i=1;i*i 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | char s11[1000],s22[1000]; 7 | while(scanf("%s%s",&s11,&s22) !=EOF){ 8 | string s1=s11; 9 | string s2=s22; 10 | vector v1(128,0), v2(128,0); 11 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | long long int n; 9 | cin>>n; 10 | long long int ans = n*(n+1); 11 | long long int p; 12 | if(n%2) p = n*((n-1)/2); 13 | else p = (n/2)*(n-1); 14 | ans+=p; 15 | cout< 2 | using namespace std; 3 | 4 | int convert(string &s){ 5 | int v=s[0]-'0'; 6 | v*=10; 7 | v+= s[1]-'0'; 8 | v*= pow(10,s[3]-'0'); 9 | return v; 10 | } 11 | 12 | int main(){ 13 | ios_base::sync_with_stdio(false); 14 | while(true){ 15 | string s; 16 | cin>>s; 17 | int n = convert(s); 18 | if(n==0) break; 19 | int x = log2(n); 20 | int y = pow(2,x); 21 | int diff = n-y; 22 | cout<< 1 + diff*2 << endl; 23 | } 24 | } -------------------------------------------------------------------------------- /spoj/datingrishi.cpp: -------------------------------------------------------------------------------- 1 | #define __USE_MINGW_ANSI_STDIO 0 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | using namespace std; 20 | 21 | #define PI atan2(0, -1) 22 | #define epsilon 0.000000001 23 | #define INF 5000000000000000000 24 | #define MOD 1000000007 25 | 26 | int kase, N, heights [100010], lo, hi; 27 | long long ret; 28 | 29 | int main(){ 30 | //freopen("fencing.in", "r", stdin); freopen("fencing.out", "w", stdout); 31 | ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(18); 32 | cin >> kase; 33 | for(int kk = 1; kk <= kase; kk++){ 34 | cin >> N; lo = 0; hi = N-1; ret = 0; 35 | for(int i = 0; i < N; i++) cin >> heights[i]; 36 | while(lo < hi){ 37 | if(heights[lo] > heights[hi]){ 38 | ret = max(ret, (long long)heights[hi]*(long long)(hi-lo)); 39 | hi--; 40 | } 41 | else{ 42 | ret = max(ret, (long long)heights[lo]*(long long)(hi-lo)); 43 | lo++; 44 | } 45 | } 46 | cout << ret << '\n'; 47 | } 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /spoj/diehard.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | int t; 7 | cin>>t; 8 | while(t--){ 9 | int h,a; 10 | cin>>h>>a; 11 | if(h==0 || a==0) { 12 | cout<<0<0 && a>0){ 19 | if(h>5 && a>10){ 20 | h-=2; 21 | a-=8; 22 | } 23 | else if(h>20 && a>0){ 24 | h-=17; 25 | a+=7; 26 | } 27 | else{ 28 | cout< 2 | #include 3 | #define sz(a) int((a).size()) 4 | #define pb push_back 5 | #define all(c) (c).begin(),(c).end() 6 | #define tr(c,i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++) 7 | #define present(c,x) ((c).find(x) != (c).end()) 8 | #define cpresent(c,x) (find(all(c),x) != (c).end()) 9 | using namespace std; 10 | 11 | typedef vector vi; 12 | typedef vector vvi; 13 | typedef pair ii; 14 | typedef vector vii; 15 | typedef vector vvii; 16 | 17 | int main(){ 18 | int t; 19 | cin>>t; 20 | while(t--){ 21 | string s; 22 | cin>>s; 23 | unordered_map m; 24 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | int t; 7 | cin>>t; 8 | while(t--){ 9 | int n,m,d; 10 | cin>>n>>m>>d; 11 | int count=0; 12 | for(int i=0;i>v; 15 | int mod = v%d; 16 | v/=d; 17 | if(mod==0 && v>0) v--; 18 | count+=v; 19 | } 20 | if(count>=m) cout<<"YES"< 2 | 3 | using namespace std; 4 | 5 | int m[9][9]; 6 | 7 | bool valid(int x, int y, int n) { 8 | for (int i = 0; i < 9; i++) 9 | if (m[i][y] == n) 10 | return false; 11 | 12 | for (int i = 0; i < 9; i++) 13 | if (m[x][i] == n) 14 | return false; 15 | 16 | for (int i = 0; i < 3; i++) 17 | for (int j = 0; j < 3; j++) 18 | if (m[i+(x-(x%3))][j+(y-(y%3))] == n) 19 | return false; 20 | 21 | return true; 22 | } 23 | 24 | bool solve(int m[][9]) { 25 | bool poss = true; 26 | int x, y; 27 | for (int i = 0; i < 9; i++) 28 | for (int j = 0; j < 9; j++) 29 | if (m[i][j] == 0) { 30 | x = i; 31 | y = j; 32 | poss = false; 33 | goto a; 34 | } 35 | a: 36 | if (poss) 37 | return true; 38 | else { 39 | for (int i = 1; i <= 9; i++) { 40 | if (valid(x, y, i)) { 41 | m[x][y] = i; 42 | 43 | if (solve(m)) 44 | return true; 45 | else 46 | m[x][y] = 0; 47 | } 48 | } 49 | 50 | return false; 51 | } 52 | } 53 | 54 | int main() { 55 | int n; 56 | scanf("%d", &n); 57 | 58 | while (n--) { 59 | for (int i = 0; i < 9; i++) 60 | for (int j = 0; j < 9; j++) 61 | scanf("%d", &m[i][j]); 62 | scanf("\n"); 63 | 64 | if (solve(m)) { 65 | for (int i = 0; i < 9; i++) { 66 | for (int j = 0; j < 9; j++) 67 | printf("%d ", m[i][j]); 68 | printf("\n"); 69 | } 70 | } else 71 | printf("No solution\n"); 72 | } 73 | 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /spoj/ec_conb.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | string to_binary(int n){ 5 | string s; 6 | while(n){ 7 | if(n%2==1) s.append("1"); 8 | else s.append("0"); 9 | n/=2; 10 | } 11 | return s; 12 | } 13 | 14 | int to_int(string &s){ 15 | int num=0; 16 | int i=0; 17 | while(i>t; 34 | for(int k=0;k>v; 37 | if(v%2==1) { 38 | cout< 2 | using namespace std; 3 | 4 | int mn(int a,int b,int c){ 5 | if (a>t; 16 | while(t--){ 17 | string s1,s2; 18 | cin>>s1>>s2; 19 | int l1=s1.size(); 20 | int l2=s2.size(); 21 | vector m[l1+1]; 22 | for(int i=0;i<=l1;i++) m[i].resize(l2+1); 23 | for(int i=0;i<=l1;i++){ 24 | for(int j=0;j<=l2;j++){ 25 | if(i==0) { 26 | m[i][j]=j; 27 | continue; 28 | } 29 | if(j==0) { 30 | m[i][j]=i; 31 | continue; 32 | } 33 | if(s1[i-1]==s2[j-1]) { 34 | m[i][j]= m[i-1][j-1]; 35 | } 36 | else m[i][j]= mn(m[i-1][j],m[i][j-1],m[i-1][j-1])+1; 37 | } 38 | } 39 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | int n; 7 | cin>>n; 8 | int a=0,b=0,c=0; 9 | for(int i=0;i>t; 12 | if(t=="1/4") a++; 13 | else if(t=="1/2") b++; 14 | else c++; 15 | } 16 | int ans=1; 17 | ans+=c; 18 | a-=c; 19 | ans+=b/2; 20 | b=b-(b/2)*2; 21 | if(b){ 22 | ans+=1; 23 | a=a-2; 24 | } 25 | if(a>0) { 26 | ans+=a/4; 27 | a=a-(a/4)*4; 28 | if(a) ans++; 29 | } 30 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | long long int k; 9 | cin>>k; 10 | long long int a=192; 11 | long long int b=442; 12 | if(k==1) cout< 2 | using namespace std; 3 | 4 | int gcd(int a,int b){ 5 | if(a>t; 14 | while(t--){ 15 | int a,b; 16 | cin>>a>>b; 17 | int x=gcd(a,b); 18 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | int n; 9 | cin>>n; 10 | int result=n; 11 | for(int i=2;i*i<=n;i++){ 12 | if (n%i==0){ 13 | result -= result/i; 14 | while(n%i==0) n/=i; 15 | } 16 | } 17 | cout<0: 5 | n=int(raw_input()) 6 | result=n 7 | r = int(math.sqrt(n)) 8 | for i in range(2,r+1): 9 | if n%i==0: 10 | #print "prime",i 11 | result *= (1.0 - 1.0/i) 12 | while n%i==0: 13 | n/=i 14 | if n>1: 15 | result*=(1.0 - 1.0/n); 16 | print int(result) -------------------------------------------------------------------------------- /spoj/facefrnd.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | int n; 7 | cin>>n; 8 | map m; 9 | while(n--){ 10 | int f; 11 | cin>>f; 12 | m[f]=1; 13 | int t; 14 | cin>>t; 15 | while(t--){ 16 | int num; 17 | cin>>num; 18 | if(m.find(num)==m.end()){ 19 | m[num]=2; 20 | } 21 | } 22 | } 23 | map::iterator i; 24 | int c=0; 25 | for(i=m.begin();i!=m.end();i++){ 26 | if(i->second==2) c++; 27 | } 28 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | int t; 7 | cin>>t; 8 | int i=1; 9 | while(t--){ 10 | int n; 11 | cin>>n; 12 | if(n==0) { 13 | cout<<"Case "<>inc; 18 | for(int i=1;i>c; 21 | long long int t_inc = exc+c; 22 | exc = max(inc,exc); 23 | inc = t_inc; 24 | } 25 | cout<<"Case "< 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | int n; 9 | cin>>n; 10 | int* a = new int[n]; 11 | int* b = new int[n]; 12 | for(int i=0;i>a[i]; 14 | } 15 | for(int i=0;i>b[i]; 17 | } 18 | sort(a,a+n); 19 | sort(b,b+n); 20 | int ans=0; 21 | for(int i=0;i 3 | using namespace std; 4 | 5 | int factZeros(int n){ 6 | int ans=0; 7 | int t=5; 8 | while(t<=n){ 9 | ans += n/t; 10 | t=t*5; 11 | } 12 | return ans; 13 | } 14 | 15 | int main(){ 16 | int t; 17 | cin>>t; 18 | while(t--){ 19 | int n; 20 | cin>>n; 21 | cout< 3 | #include 4 | using namespace std; 5 | 6 | void factorial(int n){ 7 | if(n<2) { 8 | cout<<1< v; 12 | v.push_back(1); 13 | for(int i=2;i<=n;i++){ 14 | int size=v.size(); 15 | int carry=0; 16 | for(int j=0;j=0;i--){ 29 | cout<>t; 37 | while(t--){ 38 | int n; 39 | cin>>n; 40 | factorial(n); 41 | } 42 | return 0; 43 | } -------------------------------------------------------------------------------- /spoj/fibosum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int md(long long int n){ 5 | return n%1000000007; 6 | } 7 | 8 | void multiply(long long int f[2][2],long long int m[2][2]){ 9 | long long int w = md(f[0][0] * m[0][0]) + md(f[0][1] * m[1][0]); 10 | long long int x = md(f[0][0] * m[0][1]) + md(f[0][1] * m[1][1]); 11 | long long int y = md(f[1][0] * m[0][0]) + md(f[1][1] * m[1][0]); 12 | long long int z = md(f[1][0] * m[0][1]) + md(f[1][1] * m[1][1]); 13 | 14 | f[0][0]=md(w); 15 | f[0][1]=md(x); 16 | f[1][0]=md(y); 17 | f[1][1]=md(z); 18 | } 19 | 20 | void power(long long int f[2][2], int n){ 21 | if(n==0 || n==1) return; 22 | long long int m[2][2] = {{1,1},{1,0}}; 23 | power(f,n/2); 24 | multiply(f,f); 25 | if(n%2) multiply(f,m); 26 | } 27 | 28 | int fib(int n){ 29 | if(n==0) return 0; 30 | long long int f[2][2] = {{1,1},{1,0}}; 31 | power(f,n-1); 32 | return int(f[0][0]); 33 | } 34 | 35 | int main(){ 36 | ios_base::sync_with_stdio(false); 37 | int t; 38 | cin>>t; 39 | while(t--){ 40 | int n,m; 41 | cin>>n>>m; 42 | int x = fib(n+1)-1; 43 | int y = fib(m+2)-1; 44 | int z = y-x; 45 | if(z<0) z+=1000000007; 46 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | vector a(10); 7 | for(int i=0;i<10;i++) scanf("%d", &a[i]); 8 | int v=0,d=100; 9 | for(int i=0;i<10;i++){ 10 | v+=a[i]; 11 | int dd=100-v; 12 | if(abs(dd)<=abs(d)){ 13 | d=dd; 14 | } 15 | else { 16 | v-=a[i]; 17 | break; 18 | } 19 | } 20 | printf("%d\n",v); 21 | } -------------------------------------------------------------------------------- /spoj/gss1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void createTree(vector &t, vector &v,int treeIndex, int l,int r){ 5 | if(l==r){ 6 | t[treeIndex]=v[l]; 7 | return t[treeIndex]; 8 | } 9 | int mid=(l+r)/2; 10 | createTree(t,v,2*treeIndex+1,l,mid); 11 | createTree(t,v,2*treeIndex+2,mid+1,r); 12 | t[treeIndex] = max(t[treeIndex*2+1],t[treeIndex*2+2]); 13 | } 14 | 15 | int getQ(vector &t,int treeIndex,int l,int r, int ql,int qr){ 16 | if(l>=ql && r<=qr) return t[treeIndex]; 17 | int mid = (l+r)/2; 18 | if(ql>mid) return getQ(t,2*treeIndex+2,mid+1,r,ql,qr); 19 | else if(qr<=mid) return getQ(t,2*treeIndex+1,l,mid,ql,qr); 20 | else return max(getQ(t,2*treeIndex+1,l,mid,ql,qr) , 21 | getQ(t,2*treeIndex+2,mid+1,r,ql,qr)); 22 | } 23 | 24 | int main(){ 25 | ios_base::sync_with_stdio(false); 26 | int n; 27 | cin>>n; 28 | vector v; 29 | int k=n; 30 | while(k--){ 31 | int t; 32 | cin>>t; 33 | v.push_back(t); 34 | } 35 | int m; 36 | cin>>m; 37 | vector tree(4*n); 38 | createTree(tree,v,0,0,n-1); 39 | while(m--){ 40 | int i,j; 41 | cin>>i>>j; 42 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | int n,k; 7 | cin>>n>>k; 8 | vector a(n); 9 | for(int i=0;i>a[i]; 11 | } 12 | sort(a.begin(), a.end()); 13 | int count=0; 14 | int i=0,j=1; 15 | while(j 2 | using namespace std; 3 | 4 | int main(){ 5 | while(true){ 6 | double n; 7 | cin>>n; 8 | if(n==0.00) break; 9 | else{ 10 | int count=1; 11 | double sum=0.5; 12 | double x=3.0; 13 | while(sum 2 | using namespace std; 3 | 4 | #define INF (int)1e9 5 | #define EPS 1e-9 6 | #define MAX 100010 7 | 8 | typedef pair pii; 9 | typedef vector vi; 10 | typedef vector vii; 11 | 12 | vii G[MAX]; 13 | int d[MAX], e[MAX]; 14 | 15 | void solution(int n, int m, int start, int end) 16 | { 17 | int i, j, u, v, wt, b, a; 18 | for(int i = 0; i <= n; i++){ 19 | G[i].clear(); 20 | d[i] = INF; 21 | e[i] = 0; 22 | } 23 | while(m--){ 24 | scanf("%d %d %d", &u, &v, &wt); 25 | G[u].push_back(pii(wt, v)); 26 | G[v].push_back(pii(wt, u)); 27 | } 28 | d[start] = 0; 29 | priority_queue< pii, vector< pii >, greater< pii > > Q; 30 | Q.push(pii(0, start)); 31 | 32 | while(!Q.empty()) { 33 | u = Q.top().second; 34 | b = Q.top().first; 35 | Q.pop(); 36 | int size = G[u].size(); 37 | for(i = 0; i < size; i++){ 38 | v = G[u][i].second; 39 | a = G[u][i].first; 40 | if(e[v] == 0 && b + a <= d[v]) { 41 | d[v] = b+a; 42 | Q.push(pii(d[v], v)); 43 | } 44 | } 45 | e[u] = 1; 46 | if(u == end) break; 47 | } 48 | if(d[end] == INF)printf("NONE\n"); 49 | else printf("%d\n", d[end]); 50 | } 51 | 52 | int main() 53 | { 54 | int t; 55 | scanf("%d", &t); 56 | while(t--) 57 | { 58 | int n, m, start, end; 59 | scanf("%d %d %d %d", &n, &m, &start, &end); 60 | solution(n, m, start, end); 61 | } 62 | return 0; 63 | } -------------------------------------------------------------------------------- /spoj/histogra.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | while(true){ 7 | int n; 8 | cin>>n; 9 | if(n==0) break; 10 | vector v(n+1); 11 | v[0]=0; 12 | for(int i=1;i<=n;i++) cin>>v[i]; 13 | long long int area=0; 14 | stack s; 15 | s.push(0); 16 | for(int i=1;i<=n;i++){ 17 | if(v[i]>v[s.top()]) s.push(i); 18 | else{ 19 | while(v[s.top()]>v[i]){ 20 | int t = s.top(); 21 | s.pop(); 22 | long long int t_area = (long long)v[t] * (long long)(i-s.top()-1); 23 | if(t_area>area) area=t_area; 24 | } 25 | while(!s.empty() && v[i]==v[s.top()]) s.pop(); 26 | s.push(i); 27 | } 28 | } 29 | while(s.size()>1){ 30 | int t = s.top(); 31 | s.pop(); 32 | long long int t_area = (long long)v[t] * (long long)(n+1-s.top()-1); 33 | if(t_area>area) area=t_area; 34 | } 35 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #define mod 10000000000000000LL 9 | using namespace std; 10 | int maxVal=100002; 11 | long long tree[2][100002]; 12 | 13 | long long read(int dim,int idx){ 14 | long long sum=0; 15 | while(idx!=0){ 16 | sum+=tree[dim][idx]; 17 | idx-=(idx&-idx); 18 | } 19 | return sum; 20 | } 21 | 22 | void update(int dim,int idx,long long val){ 23 | while(idx>n; 39 | cin>>q; 40 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | long long int n,m; 7 | cin>>n>>m; 8 | vector v(n); 9 | for(int i=0;i>v[i]; 10 | int i=0,j=-1; 11 | while(v[i]>m && imax) max=sum; 27 | } 28 | else if(sum>m){ 29 | sum-=v[i]; 30 | i++; 31 | } 32 | else if(sum==m) { 33 | max=sum; 34 | break; 35 | } 36 | else i++; 37 | } 38 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | int t; 7 | cin>>t; 8 | while(t--){ 9 | unsigned long long int n; 10 | cin>>n; 11 | int x; 12 | cin>>x; 13 | if(x==0) cout<<"Airborne wins."< 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | map m; 8 | m[t]=true; 9 | int count=0; 10 | while(t!=1){ 11 | int sum=0; 12 | while(t){ 13 | sum += (t%10)*(t%10); 14 | t/=10; 15 | } 16 | if(m.find(sum)!=m.end()){ 17 | cout<<-1< 2 | using namespace std; 3 | 4 | #define INF (int)1e9 5 | #define EPS 1e-9 6 | #define REP(i, a, b) for(int i = a; i < b; ++i) 7 | #define PB push_back 8 | #define MP make_pair 9 | 10 | typedef long long ll; 11 | typedef pair PII; 12 | typedef vector VI; 13 | typedef vector VS; 14 | typedef vector VII; 15 | 16 | int main() 17 | { 18 | int tt; 19 | scanf("%d", &tt); 20 | REP(qq, 0, tt){ 21 | int n, i; 22 | scanf("%d", &n); 23 | int ans = 0; 24 | if(n%3==0) i=n-3; 25 | else if((n-1)%3==0) i=n-1; 26 | else if((n-2)%3==0) i=n-2; 27 | for(;i>=3;i-=3) 28 | { 29 | if((n-i)%5==0) 30 | { 31 | ans = 1; 32 | break; 33 | } 34 | } 35 | if(ans) printf("Case %d: %d\n",qq+1,i); 36 | else printf("Case %d: -1\n",qq+1); 37 | } 38 | return 0; 39 | } -------------------------------------------------------------------------------- /spoj/incseq.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define ff first 6 | #define ss second 7 | #define mp make_pair 8 | #define memo(a,b) memset(a,b,sizeof(a)) 9 | #define INF 1e9 10 | #define EPS 1e-8 11 | #define PI 3.14159265358979323846 12 | 13 | typedef long long ll ; 14 | typedef unsigned long long ull ; 15 | 16 | /* int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; */ // 4 Direction 17 | /* int dx[] = {1,-1,0,0,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1,1,-1}; */ // 8 Direction 18 | /* int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; */ // Knight Direction 19 | /* int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; */ // Hexagonal Direction 20 | 21 | int n , k , ans = 0 , mod = 5000000 ; 22 | ll tree[51][100000+10] ; 23 | 24 | void update(int idx,int j ,ll V) 25 | { 26 | while(idx<=100005) 27 | { 28 | tree[j][idx]=(tree[j][idx]+V)%mod; 29 | idx+=idx&(-idx); 30 | } 31 | } 32 | 33 | ll query(int idx,int j) 34 | { 35 | ll sum=0; 36 | while(idx>0) 37 | { 38 | sum=(sum+tree[j][idx])%mod; 39 | idx-=idx&(-idx); 40 | } 41 | return sum; 42 | } 43 | 44 | int main() 45 | { 46 | //freopen("input.txt","r",stdin); 47 | //freopen("output.txt","w",stdout); 48 | scanf("%d%d",&n,&k); 49 | for(int j=1;j<=n;j++) 50 | { 51 | int a ; 52 | scanf("%d",&a); 53 | a++; 54 | for(int i=1;i<=k;i++) 55 | { 56 | ll p = i == 1 ? 1 : query(a-1,i-1); 57 | update(a,i,p); 58 | if(i==k) ans=(ans+p)%mod; 59 | } 60 | } 61 | cout << ans << endl ; 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /spoj/input.txt: -------------------------------------------------------------------------------- 1 | 2 2 | CCCCC 3 | ABABA -------------------------------------------------------------------------------- /spoj/invcnt.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long int merge(vector &v, int l1,int r1, int l2, int r2){ 5 | vector t; 6 | int i=l1; 7 | int j=l2; 8 | long long int count=0; 9 | while(i<=r1 && j<=r2){ 10 | if(v[i]>v[j]){ 11 | count += (r1-i+1); 12 | t.push_back(v[j]); 13 | j++; 14 | } 15 | else { 16 | t.push_back(v[i]); 17 | i++; 18 | } 19 | } 20 | while(i<=r1){ 21 | t.push_back(v[i]); 22 | i++; 23 | } 24 | while(j<=r2){ 25 | t.push_back(v[j]); 26 | j++; 27 | } 28 | int k=l1; 29 | for(int i=0;i &v, int l , int r){ 36 | if(l==r) return 0; 37 | int mid=(l+r)/2; 38 | long long int i1 = mergeSort(v,l,mid); 39 | long long int i2 = mergeSort(v,mid+1,r); 40 | long long int i3 = merge(v,l,mid,mid+1,r); 41 | //cout<>t; 49 | while(t--){ 50 | int n; 51 | cin>>n; 52 | vector v(n); 53 | for(int i=0;i>v[i]; 54 | long long int i = mergeSort(v,0,n-1); 55 | //for(int i=0;i 2 | using namespace std; 3 | 4 | int h,w; 5 | int max_l=0,m_i=-1,m_j=-1; 6 | void dfs(vector &v,vector > &visited,int i,int j,int m){ 7 | if(i>=h || i<0 || j>=w || j<0 || v[i][j]=='#') return; 8 | if(visited[i][j]==0){ 9 | if(m>max_l) { 10 | max_l=m; 11 | m_j=j; 12 | m_i=i; 13 | } 14 | visited[i][j]=1; 15 | dfs(v,visited,i+1,j,m+1); 16 | dfs(v,visited,i,j+1,m+1); 17 | dfs(v,visited,i,j-1,m+1); 18 | dfs(v,visited,i-1,j,m+1); 19 | } 20 | } 21 | 22 | int main(){ 23 | ios_base::sync_with_stdio(false); 24 | int t; 25 | cin>>t; 26 | while(t--){ 27 | cin>>w>>h; 28 | vector v(h); 29 | vector > visited(h,vector(w,0)); 30 | for(int i=0;i>v[i]; 31 | int max=0; 32 | bool temp=false; 33 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | int a,b; 9 | cin>>a>>b; 10 | if(b==0) {cout<<1< v; 12 | v.push_back(a%10); 13 | long long int p=a; 14 | while(true){ 15 | p*=a; 16 | int ld=p%10; 17 | if(ld==v[0]) break; 18 | v.push_back(ld); 19 | } 20 | int l=v.size(); 21 | b=b%l; 22 | if(b==0){ 23 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | string a; 9 | long long int b; 10 | cin>>a>>b; 11 | if(b==0) {cout<<1< v; 13 | v.push_back(a[a.size()-1]-'0'); 14 | long long int p=v[0]; 15 | while(true){ 16 | p*=v[0]; 17 | int ld=p%10; 18 | if(ld==v[0]) break; 19 | v.push_back(ld); 20 | p=ld; 21 | } 22 | int l=v.size(); 23 | b=b%l; 24 | if(b==0){ 25 | cout< 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | 6 | int main() 7 | { 8 | ll a, b; 9 | scanf("%lld %lld", &a, &b); 10 | if(a == b && a%2 == 0)printf("0\n"); 11 | else{ 12 | if(a%2 == 0)a++; 13 | ll n = ((b-a)/2)+1; 14 | printf("%lld\n", (n*(2*a + (n-1)*2))/2); 15 | } 16 | return 0; 17 | } -------------------------------------------------------------------------------- /spoj/major.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | int t; 7 | cin>>t; 8 | while(t--){ 9 | int n; 10 | cin>>n; 11 | map m; 12 | for(int i=0;i>t; 15 | if(m.find(t)==m.end()) m[t]=1; 16 | else m[t]+=1; 17 | } 18 | n = n/2; 19 | map::iterator i; 20 | bool temp=true; 21 | for(i=m.begin();i!=m.end();i++){ 22 | if((i->second)>n) { 23 | cout<<"YES "<first< 2 | using namespace std; 3 | 4 | long long int nCr(long long int n, long long int r){ 5 | if(r>n/2) r = n-r; 6 | long long int ans=1; 7 | for(int i=1;i<=r;i++){ 8 | ans*= (n-r+i); 9 | ans/= i; 10 | } 11 | return ans; 12 | } 13 | 14 | int main(){ 15 | ios_base::sync_with_stdio(false); 16 | int t; 17 | cin>>t; 18 | while(t--){ 19 | long long int n,k; 20 | cin>>n>>k; 21 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | int i=1; 8 | while(i<=t){ 9 | long long int n; 10 | cin>>n; 11 | long long int ans = 4*n*n; 12 | cout<<"Case "< 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | int m,k,l; 7 | cin>>k>>l>>m; 8 | vector v(1000001,-1); 9 | v[0]=false; 10 | v[1]=true; 11 | for(int i=2;i<1000001;i++){ 12 | v[i] = !v[i-1]; 13 | if((i>=k && !v[i-k]) || (i>=l && !v[i-l])) v[i]=true; 14 | } 15 | while(m--){ 16 | int n; 17 | cin>>n; 18 | if(v[n]) cout<<"A"; 19 | else cout<<"B"; 20 | } 21 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | while(true){ 7 | string s; 8 | getline(cin,s); 9 | if(s[0]=='*') break; 10 | transform(s.begin() ,s.end() ,s.begin() ,::tolower); 11 | int i=0; 12 | char c = s[0]; 13 | bool ans=true; 14 | while(i 2 | using namespace std; 3 | 4 | int spd(int s,int e, vector > &g){ 5 | 6 | } 7 | 8 | int main(){ 9 | ios_base::sync_with_stdio(false); 10 | int n,e,t,m; 11 | cin>>n>>e>>t>>m; 12 | vector > g(n+1); 13 | for(int i=0;i>a>>b>>c; 17 | g[a][b]=c; 18 | } 19 | int count=0; 20 | for(int i=1;i<=n;i++){ 21 | if (spd(i,e,g)<=t) count++; 22 | } 23 | } -------------------------------------------------------------------------------- /spoj/miserman.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int maxi(int a,int b,int c){ 5 | if(a>r>>c; 16 | if(c==1){ 17 | int ans=0; 18 | for(int i=0;i>tt; 21 | ans+=tt; 22 | } 23 | cout< > v(r); 27 | v[0].resize(c); 28 | for(int j=0;j>v[0][j]; 30 | } 31 | for(int i=1;i>v[i][j]; 35 | if(j==0) v[i][j] += min(v[i-1][j],v[i-1][j+1]); 36 | else if(j==c-1) v[i][j] += min(v[i-1][j],v[i-1][j-1]); 37 | else v[i][j] += maxi(v[i-1][j],v[i-1][j+1],v[i-1][j-1]); 38 | } 39 | } 40 | int ans=v[r-1][0]; 41 | for(int j=0;j 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | int n; 7 | while(scanf("%d",&n)!=EOF){ 8 | vector v(n); 9 | for(int i=0;i>v[i]; 10 | 11 | } 12 | } -------------------------------------------------------------------------------- /spoj/mrecaman.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | unordered_map m; 7 | m[0]=0; 8 | vector v(500001); 9 | v[0]=0; 10 | for(int i=1;i<=500000;i++){ 11 | int t=v[i-1] - i; 12 | if(t>0 && m.find(t)==m.end()){ 13 | } 14 | else{ 15 | t = v[i-1] + i; 16 | } 17 | m[t]=i; 18 | v[i]=t; 19 | } 20 | while(true){ 21 | int k; 22 | cin>>k; 23 | if(k==-1) break; 24 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | int memo[1000001][2]; 13 | char s[1000001]; 14 | int n; 15 | 16 | int dp(int pos,bool cambia){ 17 | if(pos==-1)return 0; 18 | if(memo[pos][cambia]!=-1)return memo[pos][cambia]; 19 | 20 | int dev=1<<30; 21 | if(cambia==0){ 22 | if(s[pos]=='A'){ 23 | dev=dp(pos-1,cambia); 24 | }else{ 25 | dev=min(1+dp(pos-1,cambia),1+dp(pos-1,!cambia)); 26 | } 27 | }else{ 28 | if(s[pos]=='B'){ 29 | dev=dp(pos-1,cambia); 30 | }else{ 31 | dev=min(1+dp(pos-1,cambia),1+dp(pos-1,!cambia)); 32 | } 33 | } 34 | 35 | memo[pos][cambia]=dev; 36 | return dev; 37 | } 38 | 39 | int main(){ 40 | 41 | //freopen("in.txt","r",stdin); 42 | //freopen("out.txt","w",stdout); 43 | 44 | while(scanf("%d",&n)==1){ 45 | scanf("%s",s); 46 | memset(memo,-1,sizeof(memo)); 47 | int x=100; 48 | while(x 2 | 3 | #define MAX 1010101 4 | 5 | using namespace std; 6 | 7 | int table[MAX]; 8 | vector occurs; 9 | 10 | void preprocess(string patt) { 11 | int i = 1, len = 0; 12 | 13 | while (i < patt.size()) { 14 | if (patt[i] == patt[len]) 15 | table[i++] = ++len; 16 | else if (len) 17 | len = table[len - 1]; 18 | else 19 | table[i++] = 0; 20 | } 21 | } 22 | 23 | void search(string patt, string txt) { 24 | int i = 0, j = 0; 25 | 26 | while (i < txt.size()) { 27 | if (patt[j] == txt[i]) 28 | i++, j++; 29 | 30 | if (j == patt.size()) { 31 | occurs.push_back(i - j); 32 | j = table[j - 1]; 33 | } else if (i < txt.size() && patt[j] != txt[i]) { 34 | if (j) 35 | j = table[j - 1]; 36 | else 37 | i++; 38 | } 39 | } 40 | } 41 | 42 | int main() { 43 | int n; 44 | string a, b; 45 | scanf("%d", &n); 46 | 47 | while (n--) { 48 | occurs.clear(); 49 | cin >> a >> b; 50 | preprocess(b); 51 | search(b, a); 52 | 53 | if (occurs.size() == 0) 54 | printf("Not Found"); 55 | else { 56 | printf("%d\n", occurs.size()); 57 | for (auto i : occurs) 58 | printf("%d ", i + 1); 59 | } 60 | 61 | printf("\n"); 62 | if (n >= 1) 63 | printf("\n"); 64 | } 65 | 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /spoj/nhay.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | char h[1000010]; 4 | char s[1000010]; 5 | int v[1000010]; 6 | 7 | void processLps(){ 8 | int n = strlen(s); 9 | int len=0; 10 | v[0]=0; 11 | int i=1; 12 | while(i 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | if(t%10==0) cout<<2< 2 | using namespace std; 3 | 4 | int main(){ 5 | int n; 6 | cin>>n; 7 | int* a = new int[1000001]; 8 | int c=0; 9 | for(int i=0;i<1000001;i++){ 10 | a[i]=c; 11 | a[i+1]=c+1; 12 | c=c+4; 13 | i++; 14 | } 15 | while(n--){ 16 | int x,y; 17 | cin>>x>>y; 18 | if(x==y){ 19 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | int n; 9 | cin>>n; 10 | map m; 11 | m["TTT"]=0; m["TTH"]=1; m["THT"]=2; m["THH"]=3; m["HTT"]=4; m["HTH"]=5; m["HHT"]=6; m["HHH"]=7; 12 | vector v(8,0); 13 | string s; 14 | cin>>s; 15 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | int t; 7 | cin>>t; 8 | vector > v(65); 9 | for(int i=0;i<65;i++) v[i].resize(10); 10 | for(int i=0;i<10;i++) { 11 | v[1][i]=10-i; 12 | } 13 | for(int i=2;i<65;i++){ 14 | v[i][9]=v[i-1][9]; 15 | } 16 | for(int i=2;i<65;i++){ 17 | for(int j=8;j>=0;j--){ 18 | v[i][j] = v[i-1][j]; 19 | v[i][j] += v[i][j+1]; 20 | } 21 | } 22 | for(int k=1;k<=t;k++){ 23 | int d,n; 24 | cin>>d>>n; 25 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | while(true){ 6 | int n,m; 7 | cin>>n>>m; 8 | if(n==0 && m==0) break; 9 | int* a = new int[n]; 10 | int* b = new int[m]; 11 | for(int i=0;i>a[i]; 12 | for(int i=0;i>b[i]; 13 | sort(a,a+n); 14 | sort(b,b+m); 15 | if(a[0] 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | int t; 7 | cin>>t; 8 | int ans=0; 9 | while(t--){ 10 | int n; 11 | cin>>n; 12 | ans^=n; 13 | } 14 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | int n; 9 | string s = "1"; 10 | cin>>n; 11 | queue > q; 12 | pair p = {s,1%n}, t; 13 | q.push(p); 14 | vector v(n,false); 15 | while(!q.empty()){ 16 | p = q.front(); 17 | q.pop(); 18 | if(p.second==0) { 19 | cout< 2 | using namespace std; 3 | 4 | bool isOperand(char i){ 5 | return i>='a' && i<='z'; 6 | } 7 | 8 | bool isOperator(char i){ 9 | return i=='+' || i=='-' || i=='*' || i=='/' || i=='^'; 10 | } 11 | 12 | int main(){ 13 | int t; 14 | cin>>t; 15 | while(t--){ 16 | string a; 17 | cin>>a; 18 | stack s; 19 | int size=a.size(); 20 | map m; 21 | m['+']=0; m['-']=1; m['*']=2; m['/']=3; m['^']=4; m['(']=5; 22 | string rpn; 23 | for(int i=0;im[s.top()]){ 33 | s.push(a[i]); 34 | } 35 | else{ 36 | while(!s.empty() && m[s.top()]>m[a[i]]){ 37 | rpn.push_back(s.top()); 38 | s.pop(); 39 | } 40 | s.push(a[i]); 41 | } 42 | } 43 | else if(a[i]==')'){ 44 | while(!s.empty() && s.top()!='('){ 45 | rpn.push_back(s.top()); 46 | s.pop(); 47 | } 48 | s.pop(); 49 | } 50 | } 51 | while(!s.empty()){ 52 | rpn.push_back(s.top()); 53 | s.pop(); 54 | } 55 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | string s; 9 | cin>>s; 10 | int l=s.size(); 11 | string a (s); 12 | int i=0; 13 | while(i=0){ 30 | a[index]='0'; 31 | a[l-1-index]='0'; 32 | index--; 33 | } 34 | if(index>=0){ 35 | a[index]++; 36 | a[l-1-index]++; 37 | } 38 | else{ 39 | a = "1" + a + "1"; 40 | } 41 | } 42 | } 43 | } 44 | else{ 45 | int index=l/2; 46 | if(s[index+1]>a[index+1]) { 47 | if(s[index]<'9'){ 48 | a[index]++; 49 | } 50 | else{ 51 | a[index]='0'; 52 | index--; 53 | while(a[index]=='9' && index>=0){ 54 | a[index]='0'; 55 | a[l-1-index]='0'; 56 | index--; 57 | } 58 | if(index>=0){ 59 | a[index]++; 60 | a[l-1-index]++; 61 | } 62 | else{ 63 | a = "1" + a + "1"; 64 | } 65 | } 66 | } 67 | if(s[index+1]==a[index+1]){ 68 | while() 69 | } 70 | } 71 | } 72 | return 0; 73 | } -------------------------------------------------------------------------------- /spoj/party.cpp: -------------------------------------------------------------------------------- 1 | //0/1-Knapsack 2 | #include 3 | using namespace std; 4 | 5 | bool mycomp(pair &p1, pair &p2){ 6 | if (p1.first == p2.first) return p1.second>b>>n; 15 | if(b==0 && n==0) break; 16 | int i=0; 17 | vector< pair > cf(n); 18 | while(i>c>>f; 21 | cf[i].first=c; 22 | cf[i].second=f; 23 | i++; 24 | } 25 | sort(cf.begin(),cf.end(),mycomp); 26 | vector< vector > m(n); 27 | for(int i=0;imax_v) { 45 | max_b=j; 46 | max_v=m[i][j]; 47 | } 48 | } 49 | } 50 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | while(true){ 6 | int n; 7 | cin>>n; 8 | if(n==0) break; 9 | vector v1(n+1,0); 10 | vector v2(n+1,0); 11 | for(int i=1;i<=n;i++){ 12 | cin>>v1[i]; 13 | v2[v1[i]]=i; 14 | } 15 | if(equal(v1.begin()+1,v1.end(),v2.begin()+1)){ 16 | cout<<"ambiguous"< 2 | using namespace std; 3 | 4 | struct node{ 5 | bool isEnd; 6 | node* p[10]; 7 | node(){ 8 | isEnd=false; 9 | for(int i=0;i<10;i++) p[i]=NULL; 10 | } 11 | }; 12 | 13 | bool addToTree(node* root, string &s){ 14 | int n=s.size(); 15 | bool ans=false; 16 | node* temp=root; 17 | for(int i=0;ip[s[i]-'0']==NULL){ 19 | temp->p[s[i]-'0'] = new node(); 20 | temp = temp->p[s[i]-'0']; 21 | } 22 | else{ 23 | temp = temp->p[s[i]-'0']; 24 | if(temp->isEnd==true) { 25 | ans=true; 26 | break; 27 | } 28 | } 29 | } 30 | temp->isEnd=true; 31 | if(ans) return ans; 32 | for(int i=0;i<10;i++) { 33 | if(temp->p[i]!=NULL) ans=true; 34 | } 35 | return ans; 36 | } 37 | 38 | int main(){ 39 | ios_base::sync_with_stdio(false); 40 | int t; 41 | cin>>t; 42 | for(int k=0;k>n; 46 | bool ans=true; 47 | bool temp=false; 48 | for(int i=0;i>s; 51 | if(ans) temp = addToTree(root,s); 52 | if(temp) ans=false; 53 | } 54 | if(ans) cout<<"YES"< 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | int t; 7 | cin>>t; 8 | while(t--){ 9 | int e,f; 10 | cin>>e>>f; 11 | int wg = f-e; 12 | int n; 13 | cin>>n; 14 | vector p(n); 15 | vector w(n); 16 | for(int i=0;i>p[i]>>w[i]; 17 | 18 | } 19 | 20 | } -------------------------------------------------------------------------------- /spoj/ppath.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | vector p(10000,true); 4 | void prime(){ 5 | for(int i=2;i*i<=10000;i++){ 6 | if(p[i]==false) continue; 7 | for(int j=i*i;j<10000;){ 8 | p[j]=false; 9 | j+=i; 10 | } 11 | } 12 | for(int i=0;i<1000;i++){ 13 | p[i]=false; 14 | } 15 | } 16 | 17 | int main(){ 18 | prime(); 19 | int t; 20 | cin>>t; 21 | while(t--){ 22 | int a,b; 23 | cin>>a>>b; 24 | queue q; 25 | q.push(a); 26 | int c=0; 27 | bool possible=false; 28 | int count=0; 29 | vector v(10000,false); 30 | v[a]=true; 31 | while(!q.empty()){ 32 | int size=q.size(); 33 | while(size--){ 34 | int k = q.front(); 35 | q.pop(); 36 | //cout< 2 | using namespace std; 3 | int main() { 4 | int t; 5 | cin>>t; 6 | for(int i=0;i>m>>n; 9 | bool* a = new bool[n-m+1]; 10 | memset(a,true,n-m+1); 11 | for(int i=2;i 2 | #define ll long long int 3 | using namespace std; 4 | 5 | 6 | int vis[100000]={0}; 7 | 8 | vector adj[10010]; 9 | 10 | void dfs(int i){ 11 | vis[i]=1; 12 | 13 | for(auto u:adj[i]){ 14 | if(!vis[u]){ 15 | dfs(u); 16 | } 17 | } 18 | } 19 | 20 | int main(){ 21 | //freopen("input.txt","r",stdin); 22 | int n,m; 23 | cin>>n>>m; 24 | 25 | if(m!=n-1){ 26 | cout<<"NO"; 27 | }else{ 28 | while(m--){ 29 | int x,y; 30 | cin>>x>>y; 31 | adj[x-1].push_back(y-1); 32 | } 33 | 34 | dfs(0); 35 | int flag=1; 36 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | int t; 7 | cin>>t; 8 | while(t--){ 9 | double a,b,c,d; 10 | cin>>a>>b>>c>>d; 11 | double s = (a+b+c+d)/2.0; 12 | double area = sqrt((s-a)*(s-b)*(s-c)*(s-d)); 13 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | int t; 7 | cin>>t; 8 | for(int k=0;k>n; 11 | vector v(n); 12 | for(int i=0;i>v[i]; 13 | long long int c=0; 14 | long long int e=0; 15 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main(){ 5 | while(true){ 6 | long long int x; 7 | cin>>x; 8 | if(x==0) break; 9 | long long int s= (x*(x+1)*(2*x+1))/6; 10 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | int t; 7 | string s; 8 | getline(cin,s); 9 | t = atoi(s.c_str()); 10 | while(t--){ 11 | int n; 12 | getline(cin,s); 13 | n = atoi(s.c_str()); 14 | vector v(n); 15 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | while(true){ 7 | int n; 8 | cin>>n; 9 | if(n==0) break; 10 | cout< 2 | using namespace std; 3 | 4 | #define REP(i, a, b) for(int i = a; i < b; ++i) 5 | 6 | int main() 7 | { 8 | int tt; 9 | scanf("%d", &tt); 10 | REP(qq, 0, tt){ 11 | int n, x, y; 12 | scanf("%d %d %d", &n, &x, &y); 13 | for(int i = 1; ;i++){ 14 | if(x*i < n){ 15 | if(x*i % y != 0)printf("%d ", x*i); 16 | } 17 | else break; 18 | } 19 | printf("\n"); 20 | } 21 | return 0; 22 | } -------------------------------------------------------------------------------- /spoj/stamps.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | int k=1; 8 | while(t--){ 9 | int s,n; 10 | cin>>s>>n; 11 | vector v; 12 | for(int i=0;i>tt; 15 | v.push_back(tt); 16 | } 17 | sort(v.begin(),v.end()); 18 | int c=0; 19 | while(s>0 && !v.empty()){ 20 | s-=v.back(); 21 | v.pop_back(); 22 | c++; 23 | } 24 | cout<<"Scenario #"< 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | while(true){ 7 | int n; 8 | cin>>n; 9 | if(n==0) break; 10 | vector a(n); 11 | for(int i=0;i>a[i]; 12 | int i=0; 13 | int ele=0; 14 | stack s1; 15 | stack s2; 16 | while(i 2 | 3 | #define MAX 10101 4 | 5 | using namespace std; 6 | 7 | vector graph[MAX]; 8 | set art; 9 | 10 | int cont[MAX], parent[MAX]; 11 | int low[MAX], L[MAX]; 12 | 13 | void dfs(int x) { 14 | cont[x] = 1; 15 | int child = 0; 16 | 17 | for (auto i : graph[x]) { 18 | if (!cont[i]) { 19 | child++; 20 | parent[i] = x; 21 | low[i] = L[i] = L[x] + 1; 22 | 23 | dfs(i); 24 | 25 | low[x] = min(low[x], low[i]); 26 | 27 | if ((parent[x] == -1 && child > 1) || 28 | (parent[x] != -1 && low[i] >= L[x])) 29 | art.insert(x); 30 | 31 | } else if (parent[x] != i) 32 | low[x] = min(low[x], L[i]); 33 | } 34 | } 35 | 36 | int main() { 37 | int n, m, a, b; 38 | 39 | while (scanf("%d %d", &n, &m) && (n || m)) { 40 | memset(parent, -1, sizeof(parent)); 41 | memset(low, 0, sizeof(low)); 42 | memset(L, 0, sizeof(L)); 43 | memset(cont, 0, sizeof(cont)); 44 | 45 | art.clear(); 46 | 47 | for (int i = 0; i <= n; ++i) 48 | graph[i].clear(); 49 | 50 | for (int i = 0; i < m; ++i) { 51 | scanf("%d %d", &a, &b); 52 | graph[a].push_back(b); 53 | graph[b].push_back(a); 54 | } 55 | 56 | dfs(1); 57 | printf("%d\n", art.size()); 58 | } 59 | 60 | return 0; 61 | } 62 | 63 | -------------------------------------------------------------------------------- /spoj/sumitr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | main(){int t,n,i,j,k,v[100][100];cin>>t;for(k=0;k>n;for(i=0;i>v[i][j];}}for(i=n-2;i>=0;i--){for(j=0;j<=i;j++){v[i][j]+=max(v[i+1][j],v[i+1][j+1]);}}cout< 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | typedef long long ll; 8 | typedef pair P; 9 | typedef vector

Vp; 10 | 11 | Vp read_vector() { 12 | int n; 13 | cin >> n; 14 | Vp v(n); 15 | for (int i = 0; i < n; ++i) { 16 | cin >> v[i].first >> v[i].second; 17 | } 18 | return v; 19 | } 20 | 21 | ll simulate(ll t, const Vp& v) { 22 | ll c = 0; 23 | for (auto& p : v) { 24 | if (p.first <= t) { 25 | c += 1 + (t - p.first) / p.second; 26 | } 27 | } 28 | return c; 29 | } 30 | 31 | ll fun(ll t, const Vp& v1, const Vp& v2) { 32 | ll lb = 0; 33 | ll ub = t + 1; 34 | while (lb < ub) { 35 | ll mid = (lb + ub) / 2; 36 | ll c1 = simulate(mid, v1); 37 | ll c2 = simulate(t - mid, v2); 38 | if (c1 > c2) { 39 | ub = mid; 40 | } else { 41 | lb = mid + 1; 42 | } 43 | } 44 | return ub - 1; 45 | } 46 | 47 | int main() { 48 | ll t; 49 | cin >> t; 50 | Vp v1 = read_vector(); 51 | Vp v2 = read_vector(); 52 | cout << fun(t, v1, v2) << endl; 53 | } 54 | -------------------------------------------------------------------------------- /spoj/template.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | int t; 7 | cin>>t; 8 | for(int k=0;k>n; 11 | vector v(n); 12 | for(int i=0;i>v[i]; 13 | 14 | } 15 | } -------------------------------------------------------------------------------- /spoj/tesser.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int t; 7 | scanf("%d", &t); 8 | while(t--) 9 | { 10 | int n; 11 | scanf("%d", &n); 12 | int a[n]; 13 | for(int i = 0; i < n; i++)scanf("%d", &a[n]); 14 | char msg[100001]; 15 | scanf("%s", msg); 16 | int l = strlen(msg); 17 | // KMP starts 18 | int flag = 0; 19 | for(int i = 1; i < n; i++){ 20 | bool found = true; 21 | flag = 0; 22 | for(int j = 0; j < l && found; j++){ 23 | if(msg[j] == 'G' && a[i+j] < a[i+j-1])found = false; 24 | else if(msg[j] == 'L' && a[i+j] > a[i+j-1])found = false; 25 | else if(msg[j] == 'E' && a[i+j] != a[i+j-1])found = false; 26 | else if (i + j >= n)found = false; 27 | } 28 | if(found){ 29 | flag = 1; 30 | break; 31 | } 32 | } 33 | if(flag)printf("YES\n"); 34 | else printf("NO\n"); 35 | } 36 | return 0; 37 | } -------------------------------------------------------------------------------- /spoj/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | // your code goes here 6 | int n; 7 | while(cin>>n&&n!=42){ 8 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | while(true){ 6 | int c; 7 | cin>>c; 8 | if(c==0) break; 9 | vector v[100]; 10 | string s; 11 | cin>>s; 12 | int size=s.size(); 13 | int r=size/c; 14 | int k=0; 15 | for(int i=0;i=0;j--){ 24 | v[i][j]=s[k++]; 25 | } 26 | } 27 | string msg; 28 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | using namespace std ; 18 | int R,C; 19 | char c[101][101]; 20 | int memo[101][101][101]; 21 | int dp(int x1,int y1,int x2,int y2){ 22 | if(x1==R || y1==C || x2==R || y2==C)return -1<<20; 23 | if(c[x1][y1]=='#' || c[x2][y2]=='#')return -1<<20; 24 | int cont=0; 25 | if(c[x1][y1]=='*')cont++; 26 | if(c[x2][y2]=='*')cont++; 27 | if(x1==x2 && y1==y2 && c[x1][y1]=='*')cont=1; 28 | if(x1==R-1 && y1==C-1 && x2==R-1 && y2==C-1){ 29 | return (c[x1][y1]=='*'); 30 | } 31 | if(memo[x1][y1][x2]!=-1)return memo[x1][y1][x2]; 32 | int dev=cont+max( max( dp(x1+1,y1,x2+1,y2),dp(x1+1,y1,x2,y2+1)), 33 | max( dp(x1,y1+1,x2+1,y2),dp(x1,y1+1,x2,y2+1) ) ) ; 34 | memo[x1][y1][x2]=dev; 35 | return dev; 36 | } 37 | int main(){ 38 | //freopen("in.txt","r",stdin); 39 | //freopen("out.txt","w",stdout); 40 | int cases; 41 | cin>>cases; 42 | for(;cases--;){ 43 | cin>>C>>R; 44 | for(int i=0;i>c[i][j]; 47 | memset(memo,-1,sizeof(memo)); 48 | cout< 2 | using namespace std; 3 | 4 | enum{rig,dwn,lft,up}; 5 | 6 | int main(){ 7 | ios_base::sync_with_stdio(false); 8 | int c; 9 | cin>>c; 10 | for(int k=0;k>n>>m; 13 | int t=0,b=n-1,l=0,r=m-1; 14 | int d; 15 | d=rig; 16 | if(n>4 && m>4) { 17 | int x = min(n,m); 18 | t=x/2 - 1; 19 | b=n-x/2; 20 | l=x/2 - 1 ; 21 | r=m-x/2; 22 | } 23 | while(t<=b && l<=r){ 24 | t++; 25 | if(t>b) break; 26 | d=dwn; 27 | r--; 28 | if(l>r) break; 29 | d=lft; 30 | b--; 31 | if(t>b) break; 32 | d=up; 33 | l++; 34 | if(l>r) break; 35 | d=rig; 36 | } 37 | if(d==up) cout<<"U"< 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | int t; 7 | cin>>t; 8 | while(t--){ 9 | unsigned long long int n; 10 | cin>>n; 11 | cout<<(n*(n+2)*(n*2+1))/8< 2 | using namespace std; 3 | 4 | int val(vector &v,int l,int r,int day){ 5 | if(l==r) return v[l]*day; 6 | int a = v[l]*day + val(v,l+1,r,day+1); 7 | int b = v[r]*day + val(v,l,r-1,day+1); 8 | return max(a,b); 9 | } 10 | 11 | int main(){ 12 | ios_base::sync_with_stdio(false); 13 | int n; 14 | cin>>n; 15 | vector v(n); 16 | for(int i=0;i>v[i]; 17 | vector m[n]; 18 | for(int i=0;i=n) break; 26 | int d = n - (j-i); 27 | m[i][j] = max(v[j]*d+m[i][j-1],v[i]*d+m[i+1][j]); 28 | } 29 | } 30 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | int t; 7 | cin>>t; 8 | vector v(1000001); 9 | for(long long int i=0;i<1000001;i++){ 10 | v[i] = i*i; 11 | } 12 | for(int k=0;k>n; 15 | int i=0,j=int(sqrt(n)); 16 | while(i<=j){ 17 | if(v[j]+v[i]>n) j--; 18 | else if(v[j]+v[i]j) cout<<"No"< 2 | using namespace std; 3 | 4 | int main(){ 5 | long long int t; 6 | cin>>t; 7 | if(t==1) cout<<"TAK"<1){ 11 | if(t%2==1) break; 12 | else t=t/2; 13 | } 14 | if(t==1) cout<<"TAK"<