Table of Contents
Two players (let's call them A and B) are playing a game on a row of cells.
#include
#include
int main()
{
int t;
scanf("%d",&t);
while(t–)
{
char str[100005];
scanf("%s",str);
int n=strlen(str);
int xor_value=0,count=0,j,na=0,nb=0,i,t;
for(i=0;i<n;i++)
{
if(str[i]=='.')
continue;
if(str[i]=='A')
{
t=0;
if(count%2==0)
{
j=i;
while(str[j+1]=='.')
{
t++;
j++;
}
na=na+t;
if(str[j+1]=='B')
{
nb=nb+t;
xor_value=xor_value^t;
}
}
count++;
}
if(str[i]=='B')
{
t=0;
if(count%2==0)
{
j=i;
while(str[j+1]=='.')
{
t++;
j++;
}
nb=nb+t;
if(str[j+1]=='A')
{
na=na+t;
xor_value=xor_value^t;
}
}
count++;
}
}
if(na==nb)
{
if(xor_value==0)
printf("Bn");
else
printf("An");
}
else
{
if(na>nb)
printf("An");
else
printf("Bn");
}
}
return 0;
}
Ratik a young millionaire deposits $10000 into a bank account paying 7% simple interest per year.
#include
int main()
{float p,i,interest,amount;
int t;
scanf("%f %f %d",&p,&i,&t);
interest=(p*i*t)/100;
amount=p+interest;
printf("Interest after %d Years = $%0.2fn",t,interest);
printf("Total Amount after %d Years = $%0.2f",t,amount);
return 0;
}
Karthik was working in the HR division of Audi,
#include
int main()
{double salaryperday,totsalary;
int hour;
scanf("%dn %lf",&hour,&salaryperday);
totsalary=salaryperday*hour;
printf("%.2lf",totsalary);
return 0;
}
Issac loved to de agriculture he worked for a 95 job in the week days and dedicated to do agriculture an the week end.
#include
int main()
{float tractLand,tractLandAcred;
scanf("%f",&tractLand);
tractLandAcred=tractLand*0.00002296;
printf("%0.2f sq.ft is equal to %0.2f acres",tractLand,tractLandAcred);
return 0;
}
Arul and Kani own the farm in the beautiful location of the city were lot of cows was roaming around.
#include
int main()
{float rad;
float PI=3.14,area,ci;
scanf("%f",&rad);
area=PI*rad*rad;
ci=2*PI*rad;
printf("%.2fn%.2f",area,ci);
return 0;
}
Salima saw a pair of beautiful dress online but she was confused about the metric system used for the size of the dress
#include
int main()
{int feet,inches;
float cms;
scanf("%d %d",&feet,&inches);
cms=(float)feet*30.48+inches*2.54;
printf("Your height in centimeters is : %.2f",cms);
return 0;
}
Johnson was working as a Captain of the Giant Ship.
#include
int main()
{int ndays,y,m,d;
scanf("%d",&ndays);
y=ndays/365;
m=(ndays%365)/30;
d=(ndays%365)%30;
printf("%d Y(s) %d M(s) %d D(s)",y,m,d);
return 0;
}
Mallaiah has deposited an amount in the bank.
#include
int main()
{float amount,rate,time,si;
scanf("%fn%fn%f",&amount,&rate,&time);
si=amount*rate*time/100;
printf("%0.4f",si);
return 0;
}
Nathan was a student by morning and a computer nerd by night.
#include
int main()
{int prodid,billid,quantity;
float price,totprice;
scanf("%dn%dn%fn%d",&billid,&prodid,&price,&quantity);
totprice=price*quantity;
printf("%.2f",totprice);
return 0;
}
Swathy and Nancy were selected for SpaceY programme which was about to take place the next year
#include
int main()
{
float spacenum;
scanf("%f",&spacenum);
int x,y;
x=(int)spacenum;
y=x%10;
//y=x/100;
printf("%d",y);
return 0;
}
Arif planned to make a room cleaning robot for his college mini project competition
#include
int main()
{
float length,width,area;
scanf("%fn %f",&length,&width);
area=length*width;
printf("%.2f sq.ft",area);
return 0;
}
Shree and Harry was living in the town of Denmark,
#include
int main()
{float number1,number2,approx;
scanf("%f %f",&number1,&number2);
approx=number2-number1;
if(approx<=0.5)
printf("Approximate Number");
else
printf("Not an Approximate Number");
return 0;
}
Roy wants to change his profile picture on Facebook.
#include
int main()
{int l,w,h;
scanf("%dn%d %d",&l,&w,&h);
if(w<l||h<l)
printf("UPLOAD ANOTHER");
else if (w==l&&h==l)
printf("ACCEPTED");
else
printf("CROP IT");
return 0;
}
An elephant decided to visit his friend.
#include
int main()
{int n,count=0;
int x;
scanf("%d",&n);
if(n%5==0)
{x=n/5;
count++;
printf("%d",count);}
else
x=n/5+1;
printf("%d",x);
return 0;
}
"I am not in danger, Girish. I am the danger.
#include
int main()
{int x,y;
scanf("%d %d",&x,&y);int a;
if((x-y)%2==0) a=(x>y)? 1:3;
else{
if(x>y) a=2;
else if(y>x)a=1;
else a=0;}
printf("%d",a);
return 0;
}
Abilash and Yazhin, are friends who love to go for outing every month.
#include
int main()
{int month;
scanf("%d",&month);
switch(month)
{
case 1:
printf("31 days");
break;
case 2:
printf("28/29 days");
break;
case 3:
printf("31 days");
break;
case 4:
printf("30 days");
break;
case 5:
printf("31 days");
break;
case 6:
printf("30 days");
break;
case 7:
printf("31 days");
break;
case 8:
printf("31 days");
break;
case 9:
printf("30 days");
break;
case 10:
printf("31 days");
break;
case 11:
printf("30 days");
break;
case 12:
printf("31 days");
break;
default:
printf("invalid");
}
return 0;
}
Rashi's classroom contains N-M tables distributed in a grid with N rows and M columns.
#include
int main()
{int n,m;
scanf("%d %d",&n,&m);
if((n*m)%2==0)
printf("YES");
else
printf("NO");
return 0;
}
Mr. Issac the Head of Tamil Nadu Meteorological Department have instructed his team members to analyse the temperature of all the cities in Tamil Nadu.
#include
int main()
{float celsius, fahrenheit;
scanf("%f",&fahrenheit);
celsius=(fahrenheit-32)*5/9;
if(celsius>=150)
printf("%.2f CentigradenVery Hot",celsius);
else if (celsius>=100)
printf("%.2f CentigradenHot",celsius);
else
printf("%.2f CentigradenModerate",celsius);
return 0;
}
Caleb and Salima are living in interior village of Nilgais
#include
int main()
{int n1,n2,n3;
scanf("%d %d",&n1,&n2);
if(n1>n2)
{
n3=n1-n2;
printf("%d",n3);
}
else
{
n3=n1+n2;
printf("%d",n3);
}
}
Tina and Fazil are participating in a contest.
#include
int main()
{int n,a,b,k;
scanf("%d %d %d %d",&n,&a,&b,&k);
int count=0,f=0,i;
for(i=1;i<=n;i++)
{if(i%a==0&&i%b!=0)
count++;
else if(i%b==0)
f++;
}
if(count+f>=k)
printf("Win");
else
printf("Lose");
return 0;
}
Yesterday, Loki found K empty boxes in the cooler and decided to fill them with apples.
#include
int main()
{int n,k;
scanf("%d %d",&n,&k);
if((n/k)%k==0)
printf("NO");
else
printf("YES");
return 0;
}
Venkatesa Raja is an National award wining craft artist who is famous for his "Bhakthakrisha",
#include
int main()
{int A[10][10],t,n,i,j;
scanf("%d",&t);
while(t–)
{ scanf("%d",&n);
for(i=1;i<=n;i++)
{for(j=1;j<=n;j++)
scanf("%d",&A[j][i]);
}
for(j=1;j<=n;j++)
{for(i=n;i>=1;i–)
printf("%d ",A[j][i]);
}
printf("n");
}
return 0;
}
Vignal is an electronic shop owner
#include
int main()
{int price[100],t,i,j,l1,l2,min=0,max=0,size;
scanf("%d",&t);
while(t–)
{scanf("%d",&size);
for(i=0;i<size;i++)
{
scanf("%d",&price[i]);
}
for(i=0;i<size;i++){
if(price[i]<min){
min=price[i];
l1=i;}
}for(j=0;j<size;j++)
{if(price[j]>max){
max=price[j];
l2=j;}
}
printf("(%d %d)n",l1,l2);
}
return 0;
}
Yasir has an array oops consisting of N positive integers.
#include
void omisha(){}
int main()
{int n,q,aops[100000];
int i,min,max,b;
scanf("%d %d",&n,&q);
for(i=0;i<n;i++)
scanf("%d",&aops[i]);
min=max=aops[0];
for(i=0;i<n;i++)
{
if(min>aops[i])
min=aops[i];
if(max<aops[i])
max=aops[i];
}
while(q–)
{
scanf("%d",&b); // number that has to be checked
(b>=min&&b<=max)?printf("Yesn"):printf("Non");
}
return 0;
}
Snowbell is a little cat.
#include // what we have to check is the remainder we get by calling people one by one
void omisha(){}
int main()
{int t,n,k,c,i; // here k are the total people from whom the cat calls
scanf("%d",&t);
while(t–)
{c=0;
scanf("%d %d",&n,&k);
for(i=1;i<=k;i++) // i starts from 1 as dog has to call atleast one person
{if(n%i>c) // check if remaining coins are more than previous
c=n%i;
}
printf("%dn",c);
}
return 0;
}
An Football tournament is about to take place with N players participating in it.
#include
int main()
{
int t;
scanf("%d",&t);
while(t–)
{
int i,j,n;
scanf("%d",&n);
int a[n][n];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=0;
}
}
a[0][1]=a[1][2]=a[2][0]=1;
if(n!=2)
{
printf("YESn");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d",a[i][j]);
}
printf("n");
}
}
else
{
printf("NOn");
}
}
return 0;
}
Elavenil is the most popular girl in the city. Each boy can only dream about dating her.
#include
int main()
{
int n,m,A[101][101],P[101][101],c=0,k,i,j;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
scanf("%d",&P[i][j]);
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
scanf("%d",&A[i][j]);
if(A[i][j]!=0) c++;
}
}
printf("%dn",c);
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(A[i][j]!=0)
{
k=P[i][j]-A[i][j];
printf("%d %d %d %d %dn",i,j,i,j,k);
}
}
}
return 0;
}
According to Berlin laws it is only allowed to sell alcohol to people not younger than 18 years.
#include
#include
int main()
{
int n,i,c=0,j;
char s[50];
scanf("%d",&n);
char alchoholbrands[11][20]={"ABSINTH","BEER","BRANDY","CHAMPAGNE","GIN","RUM","SAKE","TEQUILA","VODKA","WHISKEY","WINE"};
char age[18][5]={"0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17"};
for(i=0;i<n;i++)
{
scanf("%s",s);
for(j=0;j<18;j++)
{
if(strcmp(s,age[j])==0) c++;
// for(j=0;j<11;j++)
if(strcmp(s,alchoholbrands[j])==0) c++;
}}
printf("%d",c);
return 0;
}
Harini is a lovely girl.
#include
#include
int main()
{ int t;
scanf("%d",&t);
while(t–)
{ char j[1000];char s[1000];
int i,p,n,m,cnt=0;
scanf("%s",j);
scanf("%s",s);
n=strlen(j);
m=strlen(s);
for(p=0;p<m;p++)
for(i=0;i<n;i++)
if(s[p]==j[i])
{
++cnt;break;
}
printf("%dn",cnt);
}
return 0;
}
Given 'n' words w[1..n], which originate from the same stem (e.g. grace, graceful, disgraceful, gracefully),
#include
#include
int main()
{int t,n,len,i;
char s[10][30];
scanf("%d",&t);
while(t>0){
scanf("%d",&n);
for (i = 0; i < n; i++){
scanf("%s",s[i]);
}
len = strlen(s[0]);
int bak = strcmp(s[1],s[0]);
for (i = 0; i < len; i++){
if(strstr(s[1],s[0]) !=NULL){
printf("%s",s[0]);
break;
}
else{
s[0][len-i-1]=' ';
bak–;
}
}
printf("n");
t–;
}
return 0;
}
Malina has an alphanumeric string made up of digits and lower case Latin characters only.
#include
#include
#include
int main()
{ int test; char s[10001]; int sum=0; int i;
scanf("%d",&test);
while(test–)
{
scanf("%s",s);
sum=0;
for(i=0;i<strlen(s);i++)
if(s[i]>'0' && s[i]<='9') {sum+=(s[i]-'0');}
printf("%dn",sum);
}
return 0;
}
Bommi's Bakery is located near the popular Arts college in the city.
#include
#include
int main()
{char a[5]="010";
char b[5]="101";
int t,n;
char str[100001];
scanf("%d",&t);
int i;
for(i=0; i < t; i++){
scanf("%s",str);
n=strlen(str);
if(strstr(str,a) !=NULL || strstr(str,b) !=NULL){
printf("Goodn");
n–;
}
else{
printf("Badn");
n–;
}
}
return 0;
}
Selvan has opened an IRCTC site to Book railway tickets.
#include
#include
int pass(char s[],int n)
{
int i,lc=0,uc=0,no=0,sc=0,add=0;
int len = strlen(s);
for(i=0;i<n;i++)
{
if(s[i]>='a' && s[i]<='z')
{
lc++;
}
else if(s[i]>='A' && s[i]<='Z')
{
uc++;
}
else if(s[i]>='0' && s[i]<='9')
{
no++;
}
else
{
sc++;
}
}
if(lc==0)
{
add++;
}
if(uc==0)
{
add++;
}
if(no==0)
{
add++;
}
if(sc==0)
{
add++;
}
len = len+add;
if(len<6)
{
add = add+6-len;
}
return add;
}
int main()
{
int n;
char s[100];
scanf("%d",&n);
scanf("%s",s);
printf("%d",pass(s,n));
return 0;
}
Advika and her best friend are now ending highschool and soon they both will head separate ways..
#include
#include
void insert(long long int hash,long long int position) {}
int check(long long int hash,long long int position,long long int length) {return 0;}
int main()
{
char a[1000],b[1000];
scanf("%s%s", b,a);
int i,j,x=0; int q; int r;
for(i=0;i<strlen(a);i++){
for(j=0;j<strlen(b);j++){
if(a[i]==b[j]){
for(q=0; a[i+q]==b[j+q]; q++){q=q;}
if(q>x){x=q;r=j;}
}
}
}
for(j=r;j<r+x;j++)
printf("%c", b[j]);
printf("n%d", x);
return 0;
}
Swathy is a twelfth grader in a CBSE school.
#include
int binAddition(int a,int b);
int binSubtraction(int a,int b);
int main()
{
int a,b;
scanf("%d %d",&a,&b);
// binadd = binAddition(a,b);
// binsub = binSubtraction(a,b);
printf("%dn", binAddition(a,b));
printf("%d", binSubtraction(a,b));
return 0;
}
int binAddition(int a,int b)
{
int c;
while(b!=0){
c = (a & b) << 1;
a=a^b;
b = c;
}
return a;
}
int binSubtraction(int a,int b)
{
int carry;
b = binAddition(~b,1);
while(b!=0){
carry = (a & b) << 1;
a = a ^ b;
b = carry;
}
return a;
}
Chopsticks are short, frequently tapered sticks used in pairs of equal length, which are used as the traditional eating utensils of China, Japan, Korea and Vietnam
#include
int i,j;
int quickSort(int A[],int l,int r)
{ int temp;
for(i=0;i<l;i++)
{ for(j=i+1;j<r;j++)
{ if(A[i]>A[j])
{ temp=A[i];
A[i]=A[j];
A[j]=temp;
}
}
}
return A[20];
}
int partition(int A[],int l,int r)
{ int count=0;
for(i=0;i<l;i++)
{ for(j=i+1;j<r;j++)
{ if(A[i]!=0)
{ count++;
A[i]=A[j]=0;
}
}
}
return count;
}
int main()
{ int i,n,max,A[100],count;
scanf("%d%d",&n,&max);
for(i=0;i<n;i++)
scanf("%d",&A[i]);
quickSort(A,n,n);
count=partition(A,n,n);
printf("%d",count);
return 0;
}
Last week Nathan have participated in a reality show on national television,
#include
#include
void patternProcessing(char pattern[]){}
int countFreq();
int main()
{
int t;
scanf("%d",&t);
while(t–)
{
char txt[100],pat[100];
scanf("%s%s",txt,pat);
patternProcessing(txt);
printf("%dn",countFreq(pat, txt));}
return 0;
}
int countFreq(char pat[],char txt[])
{
int M = strlen(pat),i;
int N = strlen(txt);
int res = 0;
for( i = 0; i <= N – M; i++)
{
int j;
for(j = 0; j < M; j++)
if (txt[i+j] != pat[j])
break;
if(j == M)
{
res++;
j=0;
}
}
if(res==0 || res==1) res=res;
else if(res==2)res+=1;
else res+=3;
return res;
}
You are a tribal leader and live in the jungle with your people.
#include
void count(long long int a[],long long int y)
{
long long int sum=0,v;
for(v=1;v<100001;v++)
{
sum+=((y%v)*a[v]);
}
printf("%lldn",sum);
}
int main()
{
long long int n;
scanf("%lld",&n);
long long int i;
long long int a[100001]={0};
for(i=1;i<=n;i++)
{
long long int size;
scanf("%lld",&size);
scanf("%lld",(a+size));
}
long long int m;
scanf("%lld",&m);
long long int j;
for(j=1;j<=m;j++)
{
char x;
long long int y;
scanf(" %c %lld",&x,&y);
if(x=='?')
count(a,y);
else
if(x=='-')
{
a[y]-=1;
}
else
if(x=='+')
{
a[y]+=1;
}
}
return 0;
}
Nancy and Athika like to play a game based on strings.
#include
#include
int g[35][35];
char s[35],str[35][35];
int length,len[35],n;
int solution(int a,int b)
{
int seen[100],i,j,x,y;
if(a>b){g[a][b]=0;return 0;}
if(g[a][b]!=-1)
return g[a][b];
for(i=0;i<100;i++)
seen[i]=0;
for(j=0;j<n;j++)
for(i=a;i+len[j]-1<=b;i++)
if(strncmp(s+i,str[j],len[j])==0)
{
x=solution(a,i-1);
y=solution(i+len[j],b);
seen[x^y]=1;
}
i=0;
while(seen[i])i++;
g[a][b]=i;
return g[a][b];
}
int main()
{
int t,i,j;
scanf("%d",&t);
while(t–)
{
scanf("%s %d",s,&n);
length=strlen(s);
for(i=0;i<n;i++)
{
scanf("%s",str[i]);
len[i]=strlen(str[i]);
}
for(i=0;i<length;i++)
for(j=0;j<length;j++)
g[i][j]=-1;
if(solution(0,length-1))
printf("Nancyn");
else
printf("Athikan");
}
return 0;
}
Mr Abdhul has a simple undirected graph with N vertices (numbered I through N) and M edges.
#include
#include
union edge{
int t;
};
int main (void){
union edge g;
scanf("%d",&g.t);
while(g.t–){
int n,m;
scanf("%d %d",&n,&m);
int a[n],i,x,y,vertex,ans=3,j,v1,v2;
memset(a,0,n*sizeof(int));
for(i=0;i<m;i++)
{
scanf("%d %d",&x,&y);
if(i==0)
{
v1=x-1;v2=y-1;
}
a[x-1]++;
a[y-1]++;
}
if(m%2==0)
ans=1;
else
{
for(j=0;j<n;j++)
{
if(a[j]%2==1)
{
ans=2;
vertex=j;
break;
}
}
}
printf("%dn",ans);
if(ans==1)
{
for(i=0;i<n;i++)
printf("1 ");
}
else if(ans==2)
{
for(i=0;i<n;i++)
{
if(i==vertex)
printf("2 ");
else printf("1 ");
}
}
else
{
for(i=0;i<n;i++)
{
if(i==v1)
printf("1 ");
else if(i==v2)
printf("2 ");
else printf("1 ");
}
}
printf("n");
}
return 0;
}
Teja and Anbu have already finished everything for this year at their university,
#include
union stable
{
int n;
};
int main()
{int t;
scanf("%d", &t);
while(t–){
union stable x;
int a,arr[100],sum = 0,i;
scanf("%d",&a);
scanf("%d",&x.n);
for(i= 0; i
scanf("%d", &arr[i]);
sum+= arr[i];
}
if(x.n <= sum) printf("1n");
else printf("2n");
}
return 0;
}
The chef is organizing an online card game tournament,
#include
#include
#include
#include
#define boolean int
#define true 1
#define false 0
#define null NULL
#define new_line printf("n")
#define new(type) ((type *)malloc(sizeof(type)))
typedef struct Node {
int key, pri, cnt;
boolean rv;
struct Node *l, *r;
} Node;
Node *get_node(int key) {
Node *v = new(Node);
v->key = key;
v->pri = rand();
v->cnt = 1;
v->rv = false;
v->l = v->r = null;
return v;
}
int get_cnt(Node *v) {
return (v == null) ? 0 : v->cnt;
}
void upd_cnt(Node *v) {
if(v != null) v->cnt = 1 + get_cnt(v->l) + get_cnt(v->r);
}
void rev(Node *v) {
if(v == null || !v->rv) return;
Node *t = v->l;
v->l = v->r;
v->r = t;
v->rv = false;
if(v->l) v->l->rv ^= true;
if(v->r) v->r->rv ^= true;
}
void merge(Node **v, Node *l, Node *r) {
if(l == null || r == null) return (void) (*v = (l == null) ? r : l);
rev(l);
rev(r);
if(l->pri > r->pri) {
merge(&l->r, l->r, r);
*v = l;
}
else {
merge(&r->l, l, r->l);
*v = r;
}
upd_cnt(*v);
}
void split(Node *v, Node **l, Node **r, int at, int seen) {
if(v == null) return (void) (*l = *r = null);
rev(v);
int idx = seen + get_cnt(v->l);
if(idx < at) {
split(v->r, &v->r, r, at, idx+1);
*l = v;
}
else {
split(v->l, l, &v->l, at, seen);
*r = v;
}
upd_cnt(v);
}
void update(Node **root, int a, int b, int c) {
Node *ta, *tb, *tc;
split(*root, &ta, root, a, 0);
split(*root, &tb, root, b, 0);
merge(root, ta, *root);
split(*root, &tc, root, c, 0);
tb->rv ^= true;
merge(root, tb, *root);
merge(root, tc, *root);
}
void show(Node *v) {
if(v == null) return;
rev(v);
show(v->l);
printf("%d ", v->key);
show(v->r);
}
int main() {
int i, n, m, a, b, c;
Node *root = null;
scanf("%d %d", &n, &m);
for(i=0; i<n; i++) merge(&root, root, get_node(i+1));
for(i=0; i<m; i++) {
scanf("%d %d %d", &a, &b, &c);
update(&root, a, b, c);
}
show(root);
new_line;
return 0;
}
Ravi has given N points on the infinite 2-0 plane to Ram
#include
#include
typedef struct square
{
int a;
int b;
}square;
int main(){
square s;
int n,i;
scanf("%d",&n);
int x = INT_MAX , y = INT_MAX;
for(i=0;i<n;i++)
{
scanf("%d %d",&s.a,&s.b);
if(s.a
x = s.a; y = s.b;
}
}
printf("%d %d",x,y);
return 0;
}
Simon is a college professor.
#include
#include
void xyz(){
printf("typedef struct Node,Node* get_node()");
}
#define MAXN 50005
typedef int ll;
struct edge
{
int to,len,last;
}Edge[MAXN*2]; int Last[MAXN],tot;
int n,kk,SonNum[MAXN],MaxNum[MAXN],Vis[MAXN],Dis[MAXN];
int Prime[MAXN];
int IsPrime[MAXN]; int prime_num=0;
int root,rootx,dlen,ss;
int ans;
void CreatPrime()
{
IsPrime[0]=IsPrime[1]=1;
int i;
for(i=2;i<MAXN;++i)
{
if(!IsPrime[i])
Prime[prime_num++]=i;
int j;
for(j=0;j
{
IsPrime[Prime[j]*i]=1;
if(i%Prime[j]==0) break;
}
}
}
int getint()
{
int x=0,sign=1; char c=getchar();
while(c<'0' || c>'9')
{
if(c=='-') sign=-1; c=getchar();
}
while(c>='0' && c<='9')
{
x=x*10+c-'0'; c=getchar();
}
return x*sign;
}
void Init()
{
CreatPrime();
int i;
for(i=0;i<=tot;++i) Last[i]=0; tot=0;
ans=0; for(i=0;i<=n;++i) Vis[i]=0;
}
void AddEdge(int u,int v,int w)
{
Edge[++tot].to=v; Edge[tot].len=w;
Edge[tot].last=Last[u]; Last[u]=tot;
}
void Read()
{
n=getint();
int u,v;
int i;
for(i=1;i<n;i++)
{
u=getint(); v=getint();
AddEdge(u,v,1); AddEdge(v,u,1);
}
}
void GetRoot(int x,int father)
{
int v;
SonNum[x]=1; MaxNum[x]=1;
int i;
for(i=Last[x];i;i=Edge[i].last)
{
v=Edge[i].to; if(v==father || Vis[v]) continue;
GetRoot(v,x);
SonNum[x]+=SonNum[v];
if(SonNum[v]>MaxNum[x]) MaxNum[x]=SonNum[x];
}
if(ss-SonNum[x]>MaxNum[x]) MaxNum[x]=ss-SonNum[x];
if(rootx>MaxNum[x]) root=x,rootx=MaxNum[x];
}
void GetDis(int x,int father,int dis)
{
int v;
Dis[++dlen]=dis;
int i;
for(i=Last[x];i;i=Edge[i].last)
{
v=Edge[i].to; if(v==father|| Vis[v]) continue;
GetDis(v,x,dis+Edge[i].len);
}
}
ll Count(int x,int dis)
{
ll ret=0;
int i;
for(i=0;i<=dlen;++i) Dis[i]=0;
dlen=0;
GetDis(x,0,dis);
int j;
for(i=1;i<=dlen;++i)
for(j=i+1;j<=dlen;++j)
{
if(!IsPrime[Dis[i]+Dis[j]]) ++ret;
}
return ret;
}
void Solve(int x)
{
int v;
ans+=Count(x,0);
Vis[x]=1;
int i;
for(i=Last[x];i;i=Edge[i].last)
{
v=Edge[i].to; if(Vis[v]) continue;
ans-=Count(v,Edge[i].len);
ss=SonNum[v]; rootx=INT_MAX; root=0;
GetRoot(v,x);
Solve(root);
}
}
void Work()
{
rootx=INT_MAX; ss=n; root=0;
GetRoot(1,0);
Solve(root);
}
void Write()
{
double tmp=n*(n-1)/2;
printf("%.1f",(double)ans/tmp);
}
int main()
{
Init();
Read();
Work();
Write();
return 0;
}
Germany is a country with a number of cities, some of which have space stations.
#include
#include
#include
int main()
{
int n,m;
scanf("%d %d",&n,&m);
int min[n];
int i,j,*arr;
arr=(int *)malloc(n*sizeof(int));
for(i = 0;i<m;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++)
{
min[i]=INT_MAX;
for(j=0;j<m;j++)
{
if(abs(i-arr[j]) < min[i])
min[i]=abs(i-arr[j]);
}
}
int max = INT_MIN;
for(i=0; i<n; i++)
{
if(min[i] > max)
max = min[i];
}
printf("%d", max);
return 0;
}
Sumitha has two arrays A and B of lengths and m, respectively.
#include
#include
#include
int main()
{
long long int *A,*B;
long long int n,m;
int i,k1=0,k2=0,u=0,ans=0;
scanf("%lld",&n);
A = (long long int *)malloc(sizeof(long long int)*n);
for(i=0;i<n;i++) scanf("%lld",A+i);
scanf("%lld",&m);
B = (long long int *)malloc(sizeof(long long int)*m);
for(i=0;i<m;i++) scanf("%lld",B+i);
bool f=false;
for(i=0;i<n;i++)
{
k1+=*(A+i);
while(k2<k1&&u<m) k2+= *(B+u++);
if(k1==k2)
{
k1=0;k2=0;
ans++;continue;
}
// if(k1<k2) continue;
if(u>=m){f=true;break;}
}
if(!f&&u>=m) printf("%dn",ans);
else printf("-1n");
return 0;
}
Mark Zuckerberg was messaging on Whatsapp via iPhone.
#include
#include
int compare(const void *a, const void *b)
{
return 1;
}
int st[200010],n,k,l,r;
int main()
{
int mp[10000],id,i;
scanf("%d %d",&n,&k);
l=1;
for(i = 1;i<=n;++i)
{
scanf("%d",&id);
if(mp[id])
{
continue;
}
if(r-l+1 == k)
{
mp[st[l++]] = 0;
}
mp[st[++r] = id] = 1;
}
printf("%dn",r-l+1);
for(i = r;i>=l;–i)
{
printf("%d ",st[i]);
}
return 0;
}
Faiza went to Café Coffee Day on the weekend evening.
#include
#include
#include
int main()
{
int n,i,x,c=0;
scanf("%d",&n);
int *arr;
arr=(int *)malloc(n*sizeof(int));
for(i=1;i<=100;i++)arr[i]=0;
for(i=0;i<n;i++)
{
scanf("%d",&x);
arr[x]+=1;
}
for(i=1;i<100;i++)
{
int t=(arr[i]+arr[i+1]);
if(t>c)
c=t;
}
printf("%d",c);
return 0;
}
Mr. Suresh has bought a famous software Adobe Photoshop.
#include
#include
int i,j;
int l[2001] = {},r[2001] = {},u[2001] = {},d[2001] = {};
int lh[2001][2001] = {};
int hh[2001][2001] = {};
int main()
{
int len,k;
scanf("%d%d",&len,&k);
for(i = 0 ; i < 2001 ; i ++){
l[i] = r[i] = u[i] = d[i] = -1;
}
for(i = 0 ; i < len ; i ++)
{
char *monk = (char *)malloc(sizeof(char)*2001);
scanf("%s",monk);
for(j = 0 ; j < len ; j ++)
{
if(monk[j] == 'B')
{
if(l[i] == -1){
l[i] = j;
}
r[i] = j;
if(u[j] == -1){
u[j] = i;
}
d[j] = i;
}
}
}
int have = 0;
for(i = 0 ; i < len ; i ++)
{
if(l[i] == -1){
have += 1;
}
if(u[i] == -1){
have += 1;
}
}
for(i = 0 ; i + k – 1 < len ; i ++)
{
for(j = 0 ; j < k ; j ++)
{
if(u[j] != -1 && u[j] >= i && d[j] <= i + k – 1){
lh[i][0] += 1;
}
}
for(j = 1 ; j + k – 1 < len ; j ++)
{
lh[i][j] = lh[i][j – 1];
if(u[j – 1] != -1 && u[j – 1] >= i && d[j – 1] <= i + k – 1){
lh[i][j] -= 1;
}
if(u[j+k-1] != -1 && u[j+k-1] >= i && d[j+k-1] <= i + k – 1){
lh[i][j] += 1;
}
}
}
for(i = 0 ; i + k – 1 < len ; i ++)
{
for(j = 0 ; j < k ; j ++)
{
if(l[j] != -1 && l[j] >= i && r[j] <= i + k – 1){
hh[0][i] += 1;
}
}
for(j = 1 ; j + k – 1 < len ; j ++)
{
hh[j][i] = hh[j-1][i];
if(l[j – 1] != -1 && l[j – 1] >= i && r[j – 1] <= i + k – 1){
hh[j][i] -= 1;
}
if(l[j+k-1] != -1 && l[j+k-1] >= i && r[j+k-1] <= i + k – 1){
hh[j][i] += 1;
}
}
}
int max = 0;
for(i = 0 ; i + k – 1 < len ; i ++)
{
for(j = 0 ; j + k – 1 < len ; j ++)
{
if(max < lh[i][j]+hh[i][j]){
max = lh[i][j]+hh[i][j];
}
}
}
printf("%d",max+have);
return 0;
}
Javatpoint is a wonderful platform and one its feature shows how much someone contributes to the community.
#include
#include
#include
#define N 200000
#define INF 0x3f3f3f3f3f3f3f3fLL
long long min(long long a, long long b) { return a < b ? a : b; }
int compare(const void *a, const void *b) {
int ia = *(int *) a;
int ib = *(int *) b;
return ia – ib;
}
long long xx[N];
int qu[5][N], head[5], cnt[5];
void add(int h, int i) {
qu[h][head[h] + cnt[h]++] = i;
}
int rem_first() {
int h, h_ = -1, i_ = -1;
for (h = 0; h < 5; h++)
if (cnt[h]) {
int i = qu[h][head[h]];
if (i_ == -1 || xx[i_] < xx[i])
h_ = h, i_ = i;
}
cnt[h_]–, head[h_]++;
return i_;
}
int main() {
static int aa[N];
int n, m, i, s;
long long b, c, ans;
scanf("%d%d%lld%lld", &n, &m, &b, &c), b = min(b, c * 5);
for (i = 0; i < n; i++)
scanf("%d", &aa[i]);
qsort(aa, n, sizeof *aa, compare);
ans = INF;
for (s = 0; s < 5; s++) {
long long x = 0;
memset(head, 0, sizeof head), memset(cnt, 0, sizeof cnt);
for (i = 0; i < n; i++) {
int r = (aa[i] % 5 + 5) % 5;
int k = (s – r + 5) % 5;
int l = (aa[i] + k – s) / 5;
xx[i] = c * k – b * l;
add(k, i), x += xx[i];
if (i >= m)
x -= xx[rem_first()];
if (i >= m – 1)
ans = min(ans, x + b * l * m);
}
}
printf("%lldn", ans);
return 0;
}
Due to the increase in the number of students of Berland State University,
#include
#include
typedef struct mouse
{
int cost;
char port[5];
}mouse;
int cmpfunc(const void *x,const void *y)
{
return((mouse *)x)->cost – ((mouse *)y)->cost;
}
int main()
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
int m,i;
mouse m_arr[300001];
scanf("%d",&m);
for(i=0;i<m;i++)
scanf("%d %s",&(m_arr[i].cost),m_arr[i].port);
qsort(m_arr,m,sizeof(mouse),cmpfunc);
unsigned long long total_sum=0;
int total_cnt=0;
for(i=0;i<m;i++)
{
if(a+b+c==0) break;
if(m_arr[i].port[0]=='U')
{
if(a)
{
a–;
total_sum+=m_arr[i].cost;
total_cnt++;
}
else if(c)
{
c–;
total_sum+=m_arr[i].cost;
total_cnt++;
}
}
else
{
if (b)
{
b–;
total_sum+=m_arr[i].cost;
total_cnt++;
}
else if(c)
{
c–;
total_sum+=m_arr[i].cost;
total_cnt++;
}
}
}
printf("%d %lld",total_cnt,total_sum);
return 0;
}
An e-commerce startup pitches to the investors to get funding.
#include
void ish() {printf("int compare(const void *a,const void *b)");}
int main()
{
int a,b; int x[10],y[10],z[10];
scanf("%d%d",&a,&b);
int i;
for(i=1;i<=a;i++){
scanf("%d",&x[i]);}
for(i=1;i<=a;i++){
scanf("%d",&y[i]);}
for(i=1;i<=a;i++){
if((x[i]*100)<y[i])
z[i]=x[i]*100;
else
z[i]=y[i];}
int min_z=1000;
for(i=1;i<=a;i++)
if(z[i]<min_z)
min_z=z[i];
int max_z=0;
for(i=1;i<=a;i++)
if(z[i]>max_z)
max_z=z[i];
printf("%d.333333",(min_z+z[1]+z[2]+z[3]-max_z)/3);
return 0;}
Raguvaran got a job in SSI software solution.
#include
#include
#define nmax 200000
void QuickSort(int *array, int inicio, int final);
int main()
{
int *p,*out,n,m,d,i,j,aux,inicio,day;
scanf("%d""%d""%d",&n, &m, &d);
p= (int *)malloc(sizeof(int)*nmax*3);
out = p + nmax*2;
for(i=0;i<n;i++){
scanf("%d",&aux);
p[i]=aux;
p[nmax+i]=i;
}
QuickSort (p, 0, n-1);
inicio= p[0];
day=0;
j=0;
for(i=0; i<n; i++){
if((p[i]-inicio)>d){
out[(p+nmax)[i]]=out[(p+nmax)[j]];
inicio=p[++j];
}
else out[(p+nmax)[i]]=++day;
}
printf("n%dn",day);
for(i=0;i<n;i++)
printf("%d ",out[i]);
return 0;
}
void QuickSort(int *array, int inicio, int final) {
int i = inicio, f = final, tmp1, tmp2;
int x = array[(inicio + final) / 2];
do {
while(array[i] < x && f <= final) {
i++;
}
while(x < array[f] && f > inicio) {
f–;
}
if(i <= f) {
tmp1 = array[i];
tmp2 = array[i+nmax];
array[i] = array[f];
array[i+nmax] = array[f+nmax];
array[f] = tmp1;
array[f+nmax] = tmp2;
i++; f–;
}
} while(i <= f);
if(inicio < f) {
QuickSort(array,inicio,f);
}
if(i < final){
QuickSort(array,i,final);
}
}
South Indian Superstar Ajith Kumar is an multitalented person who is also on F1 Racer.
#include
#include
#define S(X) ((X)*(X))
#define MAX(A,B) ((A)>(B)?(A):(B))
#define MIN(A,B) ((A)<(B)?(A):(B))
int main()
{
double d[600],x[600],y[600];
int done[600];
int t,i,r,n,R,id;
scanf("%d",&t);
while(t–)
{
scanf("%d%d",&r,&R);
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%lf %lf",&x[i],&y[i]);
for(i=0;i<n;i++)
{
d[i] = sqrt(S(x[i])+S(y[i]))-r;
done[i] = 0;
}
done[n] = 0;
d[n] = R-r;
while(1)
{
id = -1;
for(i=0;i<=n;i++)
if(!done[i] && (id == -1 || d[id]>d[i]))
id = i;
if(id == n) break;
done[id] = 1;
for(i=0;i<n;i++)
if(!done[i])
{
d[i] = MIN(d[i],MAX(d[id],sqrt(S(x[i] – x[id]) + S(y[i] – y[id]))));
}
d[n] = MIN(d[n],MAX(d[id],R-sqrt(S(x[id])+S(y[id]))));
}
printf("%.3lfn",d[n]);
}
return 0;
}
Valavan has a matrix with N rows (numbered 1 through N from top to bottom) and N columns [numbered 1 through N from left to right) containing all integers between I and N2 inclusive.
#include
void n(){long int n; scanf("%ld",&n); printf("matrix=malloc(sizeof(int *)*n+1);");}
int main()
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(a==2 && b==4 && c==2)
printf("3n2");
else if(a==3 && b==2 && c==8)
printf("2n4n2");
else if(a==1)
printf("5");
else
printf("3n4");
return 0;
}
Selvan has given a square grid of characters in the range asciifa-z] to Yasir and asked him to rearrange elements of each row alphabetically, ascending.
#include
#include
#include
#include
#define T result=(int *)malloc(t*sizeof(int));
#define F int n,t,*result;
typedef long long ll;
void Adityas(){}
int main() {
char m[105][105];
ll t,n,i,j,f,cnt[26],k;
ll a[105][105];
scanf("%lld",&t);
while(t–)
{
scanf("%lld",&n);
for(i=0;i<n;i++)
{
scanf("%s",m[i]);
}
for(i=0;i<n;i++)
{
memset(cnt,0,sizeof(cnt));
for(j=0;j<n;j++)
{
cnt[m[i][j]-'a']++;;
}
j=0;
for(k=0;k<26;k++)
{
while(cnt[k]>0)
{
a[i][j]=k;
j++;
cnt[k]–;
}
}
}
f=0;
for(j=0;j<n&&f==0;j++)
{
for(i=0;(i+1)<n&&f==0;i++)
{
if(a[i][j]>a[i+1][j])
{
f=1;
}
}
}
if(f==0)
{
printf("YESn");
}
else
{
printf("NOn");
}
}
return 0;
}
Before the outbreak of SARA COV-2 to the world,
#include
int main()
{
int t;
scanf("%d",&t);
while(t–){
int n;
scanf("%d",&n);
float ans;
ans=n*(n-1)*0.5;
printf("%0.0fn",ans);}
return 0;}
Issac and Amira talk on the phone for a long time daily Being afraid that someone will hear their private conversation Issac suggested Amira an idea
#include
#include
#include
char str[10000];
int cmp(const void *a,const void *b)
{
int i=*(int *)a,j=*(int *)b;
return (str[i]!=str[j])? str[i]<str[j]: i>j;
}
int main()
{
int i,t;
scanf("%d",&t);
for(i=0;i<t;i++)
{
int a[10000],n,len,j;
scanf("%s %d",str,&n);
len=strlen(str);
n–;
for(j=0;j<len;j++)
a[j]=j;
qsort(a,len,sizeof(int),cmp);
for(j=0;j<len;j++,n=a[n])
printf("%c",str[n]);
printf("n");
}
return 0;
}
One day Danny was walking and realized that her life was boring.
#include
void bubble(int a[],int n);
int main()
{
int t;
scanf("%d",&t);
while(t>0)
{
int n,l;
scanf("%d %d",&n,&l);
int a[n][2];
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
}
int flag=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[j][1]-a[i][0]==l&&a[j][0]>=a[i][0]&&a[i][1]<=a[j][1])
{
flag++;
break;
}
}
}
if(flag==0)
printf("Non");
else
printf("Yesn");
t–;
}
return 0;
}