LightOJ 1278 - Sum of Consecutive Integers (求一个数能被分解为等差数列的方案数)

1278 - Sum of Consecutive Integers
  PDF (English) Statistics Forum
Time Limit: 2 second(s) Memory Limit: 32 MB

Given an integer N, you have to find the number ofways you can express N as sum of consecutive integers. You have to useat least two integers.

For example, N = 15 has three solutions, (1+2+3+4+5),(4+5+6), (7+8).

Input

Input starts with an integer T (≤ 200),denoting the number of test cases.

Each case starts with a line containing an integer N (1≤ N ≤ 1014).

Output

For each case, print the case number and the number of waysto express N as sum of consecutive integers.

Sample Input

Output for Sample Input

5

10

15

12

36

828495

Case 1: 1

Case 2: 3

Case 3: 1

Case 4: 2

Case 5: 47

 



题意:给你一个数,求这个数分解为公差为1的数列的方案数

思路:原来想着枚举数列内元素个数,然后推出a1,然后验证,复杂度1e7,没想到超时了,然后就有了规律,就是这个数因子中奇数的个数(PS:是因子,不是素因子),打一个素数表优化一下,不过这个表要开的大一点






ac代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 10000010
#define LL long long
#define ll __int64
#define INF 0x7fffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-8
using namespace std;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}
//head
bool v[MAXN];
int prime[700000];
int cnt;
void db()
{
    cnt=0;v[1]=true;
    for(int i=2;i<=10000000;i++)
    {
        if(!v[i])
        {  
            prime[cnt++]=i;
            for(int j=i*2;j<=10000000;j+=i)  
            v[j]=true;
        }
    }
}
int main()
{
	db();
	LL t,cas=0;
	LL i;
	scanf("%lld",&t);
	while(t--)
	{
	    LL n;
		scanf("%lld",&n);
		LL ans=1;
		for(i=0;i<cnt&&prime[i]*prime[i]<=n;i++)
		{
			int k=0;
			if(n%prime[i]==0)
			{
				while(n%prime[i]==0)
				{
					k++;
					n/=prime[i];
				}
				if(prime[i]%2)
				ans*=(k+1);
			}
		}
		if(n>1&&n%2)//注意一下这奇数的判断
		ans*=2;
		printf("Case %lld: ",++cas); 
		printf("%lld\n",ans-1);
	}
	return 0;
}


你可能感兴趣的:(LightOJ 1278 - Sum of Consecutive Integers (求一个数能被分解为等差数列的方案数))