题意:给定一个正整数A,告知等比数列的公比为q,为这个序列能否超过一个特定的数K。
解法:该题需要考虑公比的取值,当q=1,q=-1,q=0的特殊性,由于等比数列的增长速度非常快,所以可以for循环扫描过去。
#include <cmath> #include <cstring> #include <cstdio> #include <cstdlib> #include <algorithm> #include <iostream> using namespace std; typedef long long LL; const int N = 10005; int n; int seq[N]; int k1, k2; LL K; int main() { int T, ca = 0; scanf("%d", &T); while (T--) { LL sum = 0; int c; scanf("%d %d %d %I64d", &n, &k1, &k2, &K); for (int i = 0; i < n; ++i) { scanf("%d", &c); sum += c; } printf("Case #%d: ", ++ca); if (sum > K) { puts("0"); continue; } int k3 = k1 + k2; if (k3 >= 0) { if (k3 <= 1) { puts("inf"); } else { int d = 1; while ((long double)1.0*sum <= (long double)1.0*K/k3) ++d, sum*=k3; printf("%d ", d); } } else { if (k3 == -1) { puts("inf"); } else { k3 *= -1; int d = 1; while ((long double)1.0*sum <= (long double)1.0*K/k3) ++d, sum*=k3; if (d & 1) printf("%d ", d+1); else printf("%d ", d); } } } return 0; }