1.题目描述:
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 integers b1, 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" integers a1, 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.
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.
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.
3 2 30 4 6 14 25 48
3
123 1 2143435 4 123 11 -5453 141245
0
123 1 2143435 4 54343 -13 6 124
inf
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,然后给你m个数ai和一个L;如果|bi| <= L 并且bi不等于任意一个ai,就在黑板上写下bi。让你输出黑板上bi的个数,如果写了无穷个,输入inf。
3.解题思路:
卡了很久,最后用暴力的方法交一发,居然A了。。。。。。。
其实如果仔细想想也不难,就是高中的等比数列,大概这几种情况
我们需要在解决方案中处理以下情况:
| b1 | 我的答案是0。
b1 = 0 - 如果数组a中存在0,则答案为0,否则为inf。
q = 1 - 如果b1存在于数组a中,则答案为0,否则为inf。
q = - 1 - 如果b1和b1都存在于数组a中,则答案为0,否则为inf。
q = 0 - 如果数组a中不存在0,则答案为inf,否则如果b1在回答中存在,则为0,否则answer为1。
在所有其他情况下,我们可以简单地迭代过程b的所有术语,而其绝对值不超过l。 对于每个不存在的术语,我们只需将答案加1即可。显然,每个下一个元素的绝对值至少比前一个元素的绝对值大2倍。 这就是为什么我们需要检查大多数log l进度条款。
解决方案的复杂度为O(M·logL)或O(M·logM + logL·logM)。
4.AC代码:
献上暴力模拟的代码:
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 100100
#define N 555
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
map<ll, int> mp;
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
ll b1, q, l, m;
while (~scanf("%lld%lld%lld%lld", &b1, &q, &l, &m))
{
mp.clear();
for (int i = 0; i < m; i++)
{
ll x;
scanf("%lld", &x);
mp[x] = 1;
}
ll cnt = 0;
for (int i = 0; i < N; i++)
{
if (abs(b1) > l)
break;
if (mp[b1] != 1)
cnt++;
b1 *= q;
}
if (cnt > 100)
puts("inf");
else
printf("%lld\n", cnt);
}
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return 0;
}
思维代码以后补orz