Codeforces Round #407(Div. 2)B. Masha and geometric depression【模拟+分类讨论】
2017-03-30 12:09
686 查看
B. Masha and geometric depression
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Masha really loves algebra. On the last lesson, her strict teacher Dvastan gave she new exercise.
You are given geometric progression b defined by two integers b1 and q.
Remind that a geometric progression is a sequence of integersb1, b2, b3, ...,
where for each i > 1 the respective term satisfies the condition bi = bi - 1·q,
where q is called the common ratio of the progression. Progressions in Uzhlyandia are unusual: both b1 and q can
equal 0. Also, Dvastan gave Masha m "bad"
integersa1, a2, ..., am,
and an integer l.
Masha writes all progression terms one by one onto the board (including repetitive) while condition |bi| ≤ l is
satisfied (|x| means absolute value of x).
There is an exception: if a term equals one of the "bad" integers, Masha skips it (doesn't write onto the board) and moves forward to the next term.
But the lesson is going to end soon, so Masha has to calculate how many integers will be written on the board. In order not to get into depression, Masha asked you for help: help her calculate how many numbers she will write, or print "inf"
in case she needs to write infinitely many integers.
Input
The first line of input contains four integers b1, q, l, m (-109 ≤ b1, q ≤ 109, 1 ≤ l ≤ 109, 1 ≤ m ≤ 105) —
the initial term and the common ratio of progression, absolute value of maximal number that can be written on the board and the number of "bad" integers, respectively.
The second line contains m distinct integers a1, a2, ..., am (-109 ≤ ai ≤ 109) —
numbers that will never be written on the board.
Output
Print the only integer, meaning the number of progression terms that will be written on the board if it is finite, or "inf" (without quotes) otherwise.
Examples
input
output
input
output
input
output
Note
In the first sample case, Masha will write integers 3, 12, 24. Progression term 6 will
be skipped because it is a "bad" integer. Terms bigger than 24 won't be written because they exceed l by
absolute value.
In the second case, Masha won't write any number because all terms are equal 123 and this is a "bad" integer.
In the third case, Masha will write infinitely integers 123.
题目大意:
给你几个元素,b1,q,l,m,分别表示等差数列的基数,等差公项,边界值l,以及有m个ai.
现在主人公在黑板上写下bi.bi=bi-1*q;
其中如果有aj==bi,那么这个bi就不能写下,要跳过。
直到|bi|>l为止。
问你能够写下多少个数字,如果无限写,那么输出inf.
思路:
1、如果没有各种特殊情况,那么这个问题就是一个map模拟。
2、有各种特殊情况,千万都要判定到,这里有几点:
①b1==0
②q==0
这一点注意一下,这里可能有输出1.输出0,以及输出inf的情况。
③q==1
④q==-1
Ac代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
using namespace std;
#define ll __int64
ll a[1008700];
ll Abss(ll num)
{
if(num<0)return -num;
else return num;
}
int main()
{
ll num,q,l,m;
while(~scanf("%I64d%I64d%I64d%I64d",&num,&q,&l,&m))
{
map<int ,int >s;
for(int i=0;i<m;i++)
{
scanf("%I64d",&a[i]);
s[a[i]]++;
}
if(Abss(num)>l)
{
printf("0\n");
continue;
}
if(num==0)
{
if(s[0]>0)printf("0\n");
else printf("inf\n");
continue;
}
if(q==0)
{
if(s[num]>0&&s[0]>0)printf("0\n");
else if(s[num]==0&&s[0]>0)printf("1\n");
else printf("inf\n");
continue;
}
if(q==1)
{
if(s[num]>0)printf("0\n");
else printf("inf\n");
continue;
}
if(q==-1)
{
if(s[num]>0&&s[-num]>0)printf("0\n");
else printf("inf\n");
continue;
}
int output=0;
int cnt=0;
while(Abss(num)<=l)
{
output++;
if(s[num]>0)cnt++;
num*=q;
}
printf("%d\n",output-cnt);
}
}
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Masha really loves algebra. On the last lesson, her strict teacher Dvastan gave she new exercise.
You are given geometric progression b defined by two integers b1 and q.
Remind that a geometric progression is a sequence of integersb1, b2, b3, ...,
where for each i > 1 the respective term satisfies the condition bi = bi - 1·q,
where q is called the common ratio of the progression. Progressions in Uzhlyandia are unusual: both b1 and q can
equal 0. Also, Dvastan gave Masha m "bad"
integersa1, a2, ..., am,
and an integer l.
Masha writes all progression terms one by one onto the board (including repetitive) while condition |bi| ≤ l is
satisfied (|x| means absolute value of x).
There is an exception: if a term equals one of the "bad" integers, Masha skips it (doesn't write onto the board) and moves forward to the next term.
But the lesson is going to end soon, so Masha has to calculate how many integers will be written on the board. In order not to get into depression, Masha asked you for help: help her calculate how many numbers she will write, or print "inf"
in case she needs to write infinitely many integers.
Input
The first line of input contains four integers b1, q, l, m (-109 ≤ b1, q ≤ 109, 1 ≤ l ≤ 109, 1 ≤ m ≤ 105) —
the initial term and the common ratio of progression, absolute value of maximal number that can be written on the board and the number of "bad" integers, respectively.
The second line contains m distinct integers a1, a2, ..., am (-109 ≤ ai ≤ 109) —
numbers that will never be written on the board.
Output
Print the only integer, meaning the number of progression terms that will be written on the board if it is finite, or "inf" (without quotes) otherwise.
Examples
input
3 2 30 4 6 14 25 48
output
3
input
123 1 2143435 4 123 11 -5453 141245
output
0
input
123 1 2143435 4 54343 -13 6 124
output
inf
Note
In the first sample case, Masha will write integers 3, 12, 24. Progression term 6 will
be skipped because it is a "bad" integer. Terms bigger than 24 won't be written because they exceed l by
absolute value.
In the second case, Masha won't write any number because all terms are equal 123 and this is a "bad" integer.
In the third case, Masha will write infinitely integers 123.
题目大意:
给你几个元素,b1,q,l,m,分别表示等差数列的基数,等差公项,边界值l,以及有m个ai.
现在主人公在黑板上写下bi.bi=bi-1*q;
其中如果有aj==bi,那么这个bi就不能写下,要跳过。
直到|bi|>l为止。
问你能够写下多少个数字,如果无限写,那么输出inf.
思路:
1、如果没有各种特殊情况,那么这个问题就是一个map模拟。
2、有各种特殊情况,千万都要判定到,这里有几点:
①b1==0
②q==0
这一点注意一下,这里可能有输出1.输出0,以及输出inf的情况。
③q==1
④q==-1
Ac代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
using namespace std;
#define ll __int64
ll a[1008700];
ll Abss(ll num)
{
if(num<0)return -num;
else return num;
}
int main()
{
ll num,q,l,m;
while(~scanf("%I64d%I64d%I64d%I64d",&num,&q,&l,&m))
{
map<int ,int >s;
for(int i=0;i<m;i++)
{
scanf("%I64d",&a[i]);
s[a[i]]++;
}
if(Abss(num)>l)
{
printf("0\n");
continue;
}
if(num==0)
{
if(s[0]>0)printf("0\n");
else printf("inf\n");
continue;
}
if(q==0)
{
if(s[num]>0&&s[0]>0)printf("0\n");
else if(s[num]==0&&s[0]>0)printf("1\n");
else printf("inf\n");
continue;
}
if(q==1)
{
if(s[num]>0)printf("0\n");
else printf("inf\n");
continue;
}
if(q==-1)
{
if(s[num]>0&&s[-num]>0)printf("0\n");
else printf("inf\n");
continue;
}
int output=0;
int cnt=0;
while(Abss(num)<=l)
{
output++;
if(s[num]>0)cnt++;
num*=q;
}
printf("%d\n",output-cnt);
}
}
相关文章推荐
- 【分类讨论】【set】Codeforces Round #407 (Div. 2) B. Masha and geometric depression
- 【cf789B】Masha and geometric depression(分类讨论/暴力)
- Codeforces Round #407 (Div. 2) B. Masha and geometric depression
- Codeforces Round #407 (Div. 2) B. Masha and geometric depression
- B. Masha and geometric depression----模拟
- codeforces Masha and geometric depression(set模拟,等比数列)
- B. Masha and geometric depression
- Masha and geometric depression (Codeforces-789B)
- Codeforces Round #465 (Div. 2)-C-Fifa and Fafa(分类讨论:直线与圆相交)
- 【codeforces 789B】Masha and geometric depression
- 【codechef】Devu and his Class(巧妙的 模拟,分类讨论)
- CodeForces 789B (Masha and geometric depression)
- 【分类讨论】【spfa】【BFS】Codeforces Round #416 (Div. 2) D. Vladik and Favorite Game
- Codeforces Round #379 (Div. 2) -- C. Anton and Making Potions(简单的分类讨论 + 二分)
- Codeforces789B-Masha and geometric depression
- Codeforces Round #371 (Div. 2) B. Filya and Homework(水题 分类讨论)
- 【构造】【分类讨论】Codeforces Round #435 (Div. 2) C. Mahmoud and Ehab and the xor
- 【树链剖分】【dfs序】【LCA】【分类讨论】Codeforces Round #425 (Div. 2) D. Misha, Grisha and Underground
- 【codeforce】B. Masha and geometric depression
- 【分类讨论】Codeforces Round #395 (Div. 2) D. Timofey and rectangles