Vanya has a table consisting of 100 rows, each row contains 100 cells. The rows are numbered by integers from 1 to 100 from bottom to top, the columns are numbered from 1 to 100 from left to right.
In this table, Vanya chose n rectangles with sides that go along borders of squares (some rectangles probably occur multiple times). After that for each cell of the table he counted the number of rectangles it belongs to and wrote this number into it. Now he wants to find the sum of values in all cells of the table and as the table is too large, he asks you to help him find the result.
The first line contains integer n (1 ≤ n ≤ 100) — the number of rectangles.
Each of the following n lines contains four integers x1, y1, x2, y2 (1 ≤ x1 ≤ x2 ≤ 100, 1 ≤ y1 ≤ y2 ≤ 100), where x1 and y1 are the number of the column and row of the lower left cell and x2 and y2 are the number of the column and row of the upper right cell of a rectangle.
In a single line print the sum of all values in the cells of the table.
2 1 1 2 3 2 2 3 3
10
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<stdlib.h> #include<vector> #include<stack> #include<queue> #include<map> #include<string> using namespace std; #define LL long long #define ULL unsigned long long int n; int mp[105][105]; int main(void){ scanf("%d",&n); while(n--){ int x1,y1,x2,y2; scanf("%d%d%d%d",&x1,&y1,&x2,&y2); for(int i=x1;i<=x2;i++) for(int j=y1;j<=y2;j++) mp[i][j]++; } int sum=0; for(int i=1;i<=100;i++) for(int j=1;j<=100;j++) sum+=mp[i][j]; printf("%d\n",sum); return 0; }
Vanya got an important task — he should enumerate books in the library and label each book with its number. Each of the n books should be assigned with a number from 1 to n. Naturally, distinct books should be assigned distinct numbers.
Vanya wants to know how many digits he will have to write down as he labels the books.
The first line contains integer n (1 ≤ n ≤ 109) — the number of books in the library.
Print the number of digits needed to number all the books.
13
17
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<stdlib.h> #include<vector> #include<stack> #include<queue> #include<map> #include<string> using namespace std; #define LL long long #define ULL unsigned long long int main(void){ int n; int now=1; int ten=10; int last=0; scanf("%d",&n); LL sum=0; while(n>=ten){ sum += 1LL*(ten-1-last)*now; last = ten-1; ten*=10; now++; } sum += 1LL*(n-last)*now; printf("%I64d\n",sum); return 0; }
Vanya has a scales for weighing loads and weights of masses w0, w1, w2, ..., w100 grams where w is some integer not less than 2(exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
The first line contains two integers w, m (2 ≤ w ≤ 109, 1 ≤ m ≤ 109) — the number defining the masses of the weights and the mass of the item.
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
3 7
YES
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<stdlib.h> #include<vector> #include<stack> #include<queue> #include<map> #include<string> using namespace std; #define LL long long #define ULL unsigned long long int f[10000]; int main(void){ int n,m; int now=0; scanf("%d%d",&n,&m); while(m>0){ f[now++]=m%n; m/=n; } for(int i=0;i<now;i++) if(f[i]==n-1&&n!=2){ f[i]=-1; f[i+1]++; now = max(now,i+2); } else if(f[i]>=n){ f[i+1]+=f[i]/n; f[i]%=n; now = max(now,i+2); } for(int i=0;i<now;i++) if(!(f[i]>=-1&&f[i]<=1)){ printf("NO\n"); return 0; } printf("YES\n"); return 0; }
Vanya got bored and he painted n distinct points on the plane. After that he connected all the points pairwise and saw that as a result many triangles were formed with vertices in the painted points. He asks you to count the number of the formed triangles with the non-zero area.
The first line contains integer n (1 ≤ n ≤ 2000) — the number of the points painted on the plane.
Next n lines contain two integers each xi, yi ( - 100 ≤ xi, yi ≤ 100) — the coordinates of the i-th point. It is guaranteed that no two given points coincide.
In the first line print an integer — the number of triangles with the non-zero area among the painted points.
4 0 0 1 1 2 0 2 2
3
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<stdlib.h> #include<vector> #include<stack> #include<queue> #include<math.h> #include<map> #include<string> using namespace std; #define LL long long #define ULL unsigned long long const double eps = 1e-10; const double pi = acos(-1.0); int n; struct po{ int x,y; void in(){ scanf("%d%d",&x,&y); } }node[2005],base; struct sw{ double du; }dian[2005]; int cou; int vis[2005]; double dis(po x,po y){ return sqrt((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y)); } int cmp(const void *a,const void *b){ return ((sw *)a)->du-((sw *)b)->du>0?1:-1; } int found(double du,int l,int r){ int mid ; while(l<=r){ mid = (l+r)>>1; if(dian[mid].du>=du||fabs(dian[mid].du-du)<=eps) r=mid-1; else l=mid+1; } return l; } int main(void){ scanf("%d",&n); for(int i=0;i<n;i++) node[i].in(); if(n<=2){ printf("0\n"); return 0; } LL sum = 0; for(int i=0;i<n-2;i++){ base = node[i]; cou=0; for(int j=i+1;j<n;j++){ dian[cou].du = acos((node[j].x-base.x)/dis(node[j],base)); if(node[j].y<base.y) dian[cou].du = 2*pi-dian[cou].du; cou++; } qsort(dian,cou,sizeof(dian[0]),cmp); memset(vis,0,sizeof(vis)); int j,k,tk,res; for(j=0;j<cou;j++) if(!vis[j]){ vis[j]=1; res=1; for(k=j+1;k<cou;k++) if(fabs(dian[k].du-dian[j].du)<eps){ res++; vis[k]=1; } else break; k = found(dian[j].du+pi,k,cou-1); if(k>j) for(;k<cou;k++) if(fabs(dian[k].du-dian[j].du)<eps+pi){ res++; vis[k]=1; } sum += 1LL*res*(res-1)/2; } } LL ans = 1LL *n*(n-1)*(n-2)/6-sum; printf("%I64d\n",ans); return 0; }
Vanya is doing his maths homework. He has an expression of form , where x1, x2, ..., xn are digits from 1 to 9, and sign represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
The first line contains expression s (1 ≤ |s| ≤ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
In the first line print the maximum possible value of an expression.
3+5*7+8*4
303
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<stdlib.h> #include<vector> #include<stack> #include<queue> #include<map> #include<string> using namespace std; #define LL long long #define ULL unsigned long long char str[6000]; int num[3000]; char q[3000],h[3000]; int all; LL f[3000],b[3000],t[3000]; int flast[3000],blast[3000]; LL fmut[3000],bmut[3000]; int main(void){ scanf("%s",str); all = (strlen(str)+1)/2; int now=1; q[1]='+'; for(int i=0;str[i];i++){ if(str[i]>='1'&&str[i]<='9'){ num[now]=str[i]-'0'; now++; } else { h[now-1] = str[i]; q[now] = str[i]; } } h[now-1]='+'; int flag=0,last=0; LL mut; for(int i=1;i<=all;i++) if(q[i]=='+') f[i] = f[i-1]+num[i],flag=0; else { if(!flag) flag=1,mut=num[i-1],last=i-2; flast[i]=last; fmut[i]=mut; mut*=num[i]; f[i] = f[last]+mut; } flag=0,last=all; for(int i=all;i>=1;i--) if(h[i]=='+') b[i] = b[i+1]+num[i],flag=0; else { if(!flag) flag=1,mut=num[i+1],last=i+2; blast[i]=last; bmut[i]=mut; mut*=num[i]; b[i]= b[last]+mut; } LL ans = f[all]; LL res = 0; for(int i=all;i>=1;i--){ memset(t,0,sizeof(t)); for(int j=i;j>=1;j--){ if(h[j]=='+'||j==i){ t[j]=t[j+1]+num[j]; flag=0; } else { if(!flag) flag=1,mut=num[j+1],last=j+2; mut*=num[j]; t[j]=t[last]+mut; } if(h[i]=='+'){ if(q[j]=='+') ans = max(ans,res=0LL+f[j-1]+t[j]+b[i+1]); else ans = max(ans,res=0LL+f[flast[j]]+fmut[j]*t[j]+b[i+1]); } else { if(q[j]=='+') ans = max(ans,res=0LL+f[j-1]+t[j]*bmut[i]+b[blast[i]]); else ans = max(ans,res=0LL+f[flast[j]]+fmut[j]*t[j]*bmut[i]+b[blast[i]]); } // printf("%I64d\n",res); } } printf("%I64d\n",ans); return 0; }