Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, which is based on Splay trees. Specifically, he is now studying the expose procedure.
Unfortunately, Rostislav is unable to understand the definition of this procedure, so he decided to ask programmer Serezha to help him. Serezha agreed to help if Rostislav solves a simple task (and if he doesn't, then why would he need Splay trees anyway?)
Given integers l, r and k, you need to print all powers of number k within range from l to r inclusive. However, Rostislav doesn't want to spent time doing this, as he got interested in playing a network game called Agar with Gleb. Help him!
The first line of the input contains three space-separated integers l, r and k (1 ≤ l ≤ r ≤ 1018, 2 ≤ k ≤ 109).
Print all powers of number k, that lie within range from l to r in the increasing order. If there are no such numbers, print "-1" (without the quotes).
1 10 2
1 2 4 8
2 4 5
-1
Note to the first sample: numbers 20 = 1, 21 = 2, 22 = 4, 23 = 8 lie within the specified range. The number 24 = 16 is greater then 10, thus it shouldn't be printed.
题意:输出l<=pow(k,i)<=r,中的pow(k,i);
解:可以说是水中水,但是要考虑超long long的情况,我就因此被HACK了。。。。。。。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define LL long long
int main()
{
LL l,r,k;
while(scanf("%lld%lld%lld",&l,&r,&k)!=EOF)
{
if(r<l)
{
swap(l,r);
}
int ok=0;
LL n=1;
while(n<=r)
{
if(n>=l)
{
printf("%lld ",n);
ok=1;
}
if(n>9e18/k)
{
break;
}
n*=k;
}
if(!ok)
printf("-1\n");
}
return 0;
}
It's the year 4527 and the tanks game that we all know and love still exists. There also exists Great Gena's code, written in 2016. The problem this code solves is: given the number of tanks that go into the battle from each country, find their product. If it is turns to be too large, then the servers might have not enough time to assign tanks into teams and the whole game will collapse!
There are exactly n distinct countries in the world and the i-th country added ai tanks to the game. As the developers of the game are perfectionists, the number of tanks from each country is beautiful. A beautiful number, according to the developers, is such number that its decimal representation consists only of digits '1' and '0', moreover it contains at most one digit '1'. However, due to complaints from players, some number of tanks of one country was removed from the game, hence the number of tanks of this country may not remain beautiful.
Your task is to write the program that solves exactly the same problem in order to verify Gena's code correctness. Just in case.
The first line of the input contains the number of countries n (1 ≤ n ≤ 100 000). The second line contains n non-negative integers aiwithout leading zeroes — the number of tanks of the i-th country.
It is guaranteed that the second line contains at least n - 1 beautiful numbers and the total length of all these number's representations doesn't exceed 100 000.
Print a single number without leading zeroes — the product of the number of tanks presented by each country.
3 5 10 1
50
4 1 1 10 11
110
5 0 3 1 100 1
0
In sample 1 numbers 10 and 1 are beautiful, number 5 is not not.
In sample 2 number 11 is not beautiful (contains two '1's), all others are beautiful.
In sample 3 number 3 is not beautiful, all others are beautiful.
解:分阶段做,完美的数后面有多少个0就sum+几,不完美的数记录下来,最后输出sum个0的个数。但是有些细节还是要注意,比如全是完美的情况。大体思路就是这样,其他小细节自己把握。。。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxm=1e5+10;
char s[maxm];
int OK(char *t)
{
int len=strlen(t);
int sum=0;
int ok=0;
for(int i=0; i<len; i++)
{
if(t[i]=='1')
{
sum++;
}
else if(t[i]=='0')
{
continue;
}
else
{
ok=1;
break;
}
}
if(ok==1||sum>=2)
return 0;
else
return 1;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int sum=0;
char p[maxm];
int r=0;
int cnt=0;
for(int i=0; i<n; i++)
{
scanf("%s",s);
if(strcmp(s,"0")==0)
{
r=1;
}
else if(OK(s))
{
cnt++;
sum+=(strlen(s)-1);
}
else
{
strcpy(p,s);
}
}
if(r==1)
{
printf("0\n");
}
else if(cnt==n)
{
printf("1");
for(int i=0; i<sum; i++)
printf("0");
printf("\n");
}
else
{
printf("%s",p);
for(int i=0; i<sum; i++)
printf("0");
printf("\n");
}
}
return 0;
}