【2015-2016 XVI Open CupC】【暴力】Constant Ratio 等差数列前若干项恰好为n

C. Constant Ratio
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Given an integer n, find out number of ways to represent it as the sum of two or more integers ai with the next property: ratio ai / ai - 1 is the same positive integer for all possible i > 1.

Input

Input consists of one integer n (1 ≤ n ≤ 105).

Output

Print one integer — number of representations.

Examples
input
1
output
0
input
5
output
2
input
567
output
21
Note

In the first sample no such representation exists.

In the second sample there exist two representations:

  • 1 1 1 1 1, then q = 1.
  • 1 4, then q = 4.

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 0, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int casenum, casei;
int n;
bool check(int x, int y)
{
	int k = y / x;
	LL tmp = x;
	LL sum = x;
	while(1)
	{
		tmp = tmp*k;
		sum += tmp;
		if (sum == n)return 1;
		if (sum > n)return 0;
	}
}
int main()
{
	while(~scanf("%d",&n))
	{
		int ans = 0;
		for (int x = 1; x <= n; ++x)if(n%x==0)
		{
			for (int y = x; y <= n; y+=x)
			{
				if (check(x, y))++ans;
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}
/*
【trick&&吐槽】
短的题一定要先读。水题大暴力。

【题意】
我们想找出一个数组a[],元素都为整数,个数至少为2个。
使得a[i]/a[i-1]对于i>1都为正整数

【类型】
暴力

【分析】
a[i]/a[i-1]对于i>1都为正整数
n还很小只有1e5.
我们直接暴力枚举a[1]和a[2],a[2]必须是a[1]的倍数
然后一直暴力查看和是否为n即可。
枚举a[1]、a[2]的复杂度是O(nlogn)
然后倍数倍增,当倍数>=k时,倍增次数均摊其实也不会超过10次。
于是整体的复杂度是(10nlogn),完全可以AC

【时间复杂度&&优化】
O(10nlogn)

*/


你可能感兴趣的:(codeforces,暴力,题库-CF)