Description
Davy Jones: You've been captain of the Black Pearl for 13 years. That was our agreement.
Jack: Technically I was only captain for two years, then I was mutinied upon.
Davy Jones: Then you were a poor captain, but a captain nonetheless. Have you not introduced yourself as Captain Jack Sparrow?
Jack: Technically I was only captain for two years, then I was mutinied upon.
Davy Jones: Then you were a poor captain, but a captain nonetheless. Have you not introduced yourself as Captain Jack Sparrow?
According to the Pirate Code, each of the pirates of the Caribbean at the beginning of their professional career (hereditary pirates –– at birth) is assigned by a unique identifier. Pirate's identifier is a string of four hexadecimal digits. However, it is not a usual row of numbers, it is said that personal qualities and life path of its owner are encoded in it by a mysterious way. But no one still could guess this mystical connection.
Once Captain Jack Sparrow, while sitting in captain’s cabin, decided to try to find the way to derive some data about a pirate using the identifier. Memories about how he lost the Black Pearl last time gave him the idea that more similar identifiers of two pirates are, bigger chances for these pirates to unite against the Captain, and, as a result, to make a mutiny. The Captain Jack Sparrow, of course, doesn’t want to have the mutiny on his ship, but he chose the new team this time and it is going to be a long voyage. Now Jack needs to estimate the opportunities of raising the mutiny on his ship, based on the conclusions. For this aim he first wants to know for each pair of pirates a number of positions in their identifiers in which they are different.
Input
The first line contains an integer
n –– the number of pirates aboard the Black Pearl (2 ≤
n ≤ 65536). Each of the following
n lines contains four-digit identifier of the respective pirate. Only decimal digits and lowercase Latin letters from “a” to “f” inclusive are used in writing identifiers. Identifiers of all pirates are different.
Output
Output four space separated integers –– the amount of pairs of pirates, which have different identifiers exactly in one, two, three and four positions respectively.
Sample Input
input | output |
---|---|
3 dead beef f00d | 0 0 2 1 |
#include <cstdio>
#include <iostream>
#include <cstring>
long long sum[10],ans[10],pre_num[1<<17][1<<5],n;
char s[10];
long Val(char c,long pos){
long num=isdigit(c)?(c-'0'):(c-'a'+10);
return num<<(4*pos);
}
void dfs(long pre,long now_val,long pos_val,long depth){
long tmp,pos;
for (long i=pre+1;i<=4;++i){
sum[depth]+=(pre_num[tmp=now_val+Val(s[i-1],4-i)][pos=pos_val+(1<<i)]++);
dfs(i,tmp,pos,depth+1);
}
}
int main(){
while (~scanf("%d",&n)){
memset(ans,0,sizeof(ans)); memset(pre_num,0,sizeof(pre_num));
for (long i=1;i<=n;++i){
memset(sum,0,sizeof(sum));
scanf("%s",&s); dfs(0,0,0,1);
ans[3]+=sum[3]; ans[2]+=(sum[2]=sum[2]-3*sum[3]); ans[1]+=(sum[1]-3*sum[3]-2*sum[2]);
}
ans[0]=n*(n-1)/2-ans[1]-ans[2]-ans[3];
for (long i=3;i>=0;--i){
printf("%d",ans[i]);
if (i) printf(" ");
else printf("\n");
}
}
return 0;
}
Description
Bootstrap: Aye, Captain Turner. This ship has a purpose again. And where we are bound, she cannot come. One day ashore, ten years at sea. That's a steep price for what's been done.
Will: Depends on the one day.
Will: Depends on the one day.
It's an open secret that Will Turner is the captain of the Flying Dutchman. In a distant year
A he was forced to sign a contract with the goddess Calypso, according to which Will had to go into endless sailing and he could come ashore only
B years after leaving. Moreover, the agreement allows Will to spend only one day on land, and after that he must resume sailing for another
B years.
Today is an anniversary of his departure since the formation of the contract, that’s why this day is very important for Will. Since then he has never come out on the land. To amuse himself a little, every year on this special day Will allocates
k minutes to check whether the number of the current year is a prime number, which seems a nail biting from the side. But for Will there is a special meaning in this action, because, according to the legend, at the end of the year, which number is prime, goddess Calypso can cancel one of the previously concluded treaties. Captain of the Flying Dutchman know only one way to check the number’s primality. He consequently divide it by all the natural numbers in a row, starting with 2 and ending with a number, one less than verifiable. As Will not good at math, and he count anything only once a year, dividing one number by another takes him a minute. If after
k minutes divider of the number of the year is not found, Will stops counting and considers it prime. At the end of such year he consoles himself with hope that here and now the goddess Calypso will come to him with the good news. So how many years are prime, according to Will, in the period from the first anniversary of departure to the year, when he for the first time will be able to get to shore inclusive, if goddess won’t take pity on the pirate?
Input
The only line contains space separated integers
A,
B and
k (2 ≤
A,
B ≤ 10
9; 1 ≤
k ≤ 300).
Output
Output an amount of years which numbers Will considers prime.
Sample Input
input | output |
---|---|
23 7 3 | 2 |
Hint
Will will consider numbers 25 and 29 to be prime but won't consider numbers 24, 26, 27, 28, and 30.
#include <cstdio>
#include <cstring>
#define LL long long
long l,r,pos,k,prime[400],cnt;
long ok(long x){
for (long i=2;i<x;++i)
if (x%i==0) return 0;
return 1;
}
void init(){
cnt=0;
for (long i=2;i<310;++i)
if (ok(i)) prime[++cnt]=i;
}
LL RongChi(long pre,LL now){
LL ans=0;
for (long i=pre+1;i<=cnt && prime[i]<=k+1 && prime[i]*now<=pos;++i)
ans+=(pos/(prime[i]*now))-RongChi(i,prime[i]*now);
return ans;
}
int main(){
init();
while (~scanf("%d%d%d",&l,&r,&k)){
r=l+r; l;
LL sum=0;
for (long i=1;i<=cnt && prime[i]<=k+1;++i)
if (prime[i]>l && prime[i]<=r) sum++;
pos=r;
sum+=r-RongChi(0,1);
pos=l;
sum-=l-RongChi(0,1);
printf("%I64d\n",sum);
}
return 0;
}