One day Vasya painted a Cartesian coordinate system on a piece of paper and marked some set of points(x1, y1), (x2, y2), ..., (xn, yn). Let's define neighbors for some fixed point from the given set (x, y):
- point (x', y') is (x, y)'s right neighbor, if x' > x and y' = y
- point (x', y') is (x, y)'s left neighbor, if x' < x and y' = y
- point (x', y') is (x, y)'s lower neighbor, if x' = x and y' < y
- point (x', y') is (x, y)'s upper neighbor, if x' = x and y' > y
We'll consider point (x, y) from the given set supercentral, if it has at least one upper, at least one lower, at least one left and at least one right neighbor among this set's points.
Vasya marked quite many points on the paper. Analyzing the picture manually is rather a challenge, so Vasya asked you to help him. Your task is to find the number of supercentral points in the given set.
The first input line contains the only integer n (1 ≤ n ≤ 200) — the number of points in the given set. Next n lines contain the coordinates of the points written as "x y" (without the quotes) (|x|, |y| ≤ 1000), all coordinates are integers. The numbers in the line are separated by exactly one space. It is guaranteed that all points are different.
Print the only number — the number of supercentral points of the given set.
8 1 1 4 2 3 1 1 2 0 2 0 1 1 0 1 3
2
5 0 0 0 1 1 0 0 -1 -1 0
1
In the first sample the supercentral points are only points (1, 1) and (1, 2).
In the second sample there is one supercental point — point (0, 0).
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<list>
#include<cmath>
#include<vector>
using namespace std;
const int maxn=10010;
int visx[1010];
int visxf[1010];
int visy[1010];
int visyf[1010];
struct Node{
int x,y;
}A[210];
int main()
{
int i,j,k,n;
scanf("%d",&n);
for(i=0;i<n;++i){
scanf("%d%d",&A[i].x,&A[i].y);
}int ans=0;
for(i=0;i<n;++i){
bool sign1=false,sign2=false,sign3=false,sign4=false;
for(j=0;j<n;++j){
if(j==i)continue;
if(A[j].y==A[i].y&&A[j].x>A[i].x)sign1=true;
if(A[j].y==A[i].y&&A[j].x<A[i].x)sign2=true;
if(A[j].x==A[i].x&&A[j].y>A[i].y)sign3=true;
if(A[j].x==A[i].x&&A[j].y<A[i].y)sign4=true;
}
if(sign1&&sign2&&sign3&&sign4)ans++;
}
printf("%d\n",ans);
return 0;
}
One day a highly important task was commissioned to Vasya — writing a program in a night. The program consists of n lines of code. Vasya is already exhausted, so he works like that: first he writes v lines of code, drinks a cup of tea, then he writes as much as lines, drinks another cup of tea, then he writes lines and so on: , , , ...
The expression is regarded as the integral part from dividing number a by number b.
The moment the current value equals 0, Vasya immediately falls asleep and he wakes up only in the morning, when the program should already be finished.
Vasya is wondering, what minimum allowable value v can take to let him write not less than n lines of code before he falls asleep.
The input consists of two integers n and k, separated by spaces — the size of the program in lines and the productivity reduction coefficient, 1 ≤ n ≤ 109, 2 ≤ k ≤ 10.
Print the only integer — the minimum value of v that lets Vasya write the program in one night.
7 2
4
59 9
54
In the first sample the answer is v = 4. Vasya writes the code in the following portions: first 4 lines, then 2, then 1, and then Vasya falls asleep. Thus, he manages to write 4 + 2 + 1 = 7 lines in a night and complete the task.
In the second sample the answer is v = 54. Vasya writes the code in the following portions: 54, 6. The total sum is 54 + 6 = 60, that's even more than n = 59.
二分
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<list>
#include<cmath>
#include<vector>
using namespace std;
const int maxn=10010;
long long n,k;
bool judge(long long mid){
long long ans=mid,kk=k;
while(mid/kk){
ans+=mid/kk;
kk*=k;
}
return ans>=n;
}
int main()
{
scanf("%lld%lld",&n,&k);
long long l=1,r=n*k+1,ans;
while(l<=r){
long long mid=(l+r)>>1;
if(judge(mid)){
ans=mid;
r=mid-1;
}else {
l=mid+1;
}
}
printf("%lld\n",ans);
return 0;
}
A string is binary, if it consists only of characters "0" and "1".
String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs.
You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1".
The first line contains the single integer k (0 ≤ k ≤ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters.
Print the single number — the number of substrings of the given string, containing exactly k characters "1".
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64dspecifier.
1 1010
6
2 01010
4
100 01010
0
In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010".
In the second sample the sought substrings are: "101", "0101", "1010", "01010".
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<list>
#include<cmath>
#include<vector>
using namespace std;
const int maxn=1000010;
char str[maxn];
long long num[maxn];
int main()
{
long long ans=0,k,cnt=1;
scanf("%lld%s",&k,str);
long long l=1,r,len=strlen(str),temp=0;num[0]=-1;
if(k==0){
l=0;
for(long long i=0;i<len;++i){
if(str[i]=='1'){
r=i-l;l=i+1;
ans=ans+r*(r+1)/2;
//cout<<ans<<endl;
}
}
r=(len-l);
ans=ans+r*(r+1)/2;
cout<<ans<<endl;
return 0;
}
for(long long i=0;i<len;++i){
if(str[i]=='1'){
temp++;num[cnt++]=i;
}
if(temp==k+1){
ans=ans+(num[l]-num[l-1])*(num[cnt-1]-num[cnt-2]);l++;temp--;
//cout<<num[l]-num[l-1]<<endl;
}
}
if(temp==k){
ans=ans+(num[l]-num[l-1])*(len-num[cnt-1]);
}
cout<<ans<<endl;
return 0;
}
Two integers x and y are compatible, if the result of their bitwise "AND" equals zero, that is, a & b = 0. For example, numbers 90(10110102) and 36 (1001002) are compatible, as 10110102 & 1001002 = 02, and numbers 3 (112) and 6 (1102) are not compatible, as 112 & 1102 = 102.
You are given an array of integers a1, a2, ..., an. Your task is to find the following for each array element: is this element compatible with some other element from the given array? If the answer to this question is positive, then you also should find any suitable element.
The first line contains an integer n (1 ≤ n ≤ 106) — the number of elements in the given array. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 4·106) — the elements of the given array. The numbers in the array can coincide.
Print n integers ansi. If ai isn't compatible with any other element of the given array a1, a2, ..., an, then ansi should be equal to -1. Otherwise ansi is any such number, that ai & ansi = 0, and also ansi occurs in the array a1, a2, ..., an.
2 90 36
36 90
4 3 6 3 6
-1 -1 -1 -1
5 10 6 9 8 2
-1 8 2 2 8
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<list>
#include<cmath>
#include<vector>
using namespace std;
const int maxn=1000010;
int num[maxn];
int dp[(1<<23)+1];
int main()
{
int n,i,j,k;
scanf("%d",&n);
memset(dp,-1,sizeof(dp));
for(i=1;i<=n;++i){
scanf("%d",&num[i]);
dp[num[i]^((1<<22)-1)]=num[i];//把num[i]的二进制中为1的数变为0
}
for(i=(1<<22)-1;i>=0;--i){
if(dp[i]==-1){
for(j=0;j<=22;++j){
if(dp[i|(1<<j)]!=-1){
dp[i]=dp[i|(1<<j)];//如果i中的某个二进制中的0换成1有对应则0也可以对应此数
break;
}
}
}
}
for(i=1;i<=n;++i){
if(i==n)
printf("%d\n",dp[num[i]]);
else
printf("%d ",dp[num[i]]);
}
return 0;
}