【题解】LightOJ1278 Sum of Consecutive Integers 线性筛

题目链接

Description

Given an integer N, you have to find the number of ways you can express N as sum of consecutive integers. You have to use at 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 ways to express N as sum of consecutive integers.

Sample Input

5

10

15

12

36

828495

Sample Output

Case 1: 1

Case 2: 3

Case 3: 1

Case 4: 2

Case 5: 47


等差数列求和公式化简之后,可以发现题目实际是求n的奇因子个数

#include<cstdio>
#include<cmath>
typedef long long ll;
const int N=1e7+10;
ll n,prime[N/10];
bool iscomp[N];
int t,ca,p;
void primetable()
{
    for(int i=2;i<N;i++)
    {
        if(!iscomp[i])prime[p++]=i;
        for(int j=0;j<p&&i*prime[j]<N;j++)
        {
            iscomp[i*prime[j]]=1;
            if(i%prime[j]==0)break;
        }
    } 
}
int main()
{
    //freopen("in.txt","r",stdin);
    scanf("%d",&t);
    primetable();
    while(t--)
    {
        ll ans=1;
        scanf("%lld",&n);
        for(int i=0;i<p&&prime[i]*prime[i]<=n;i++)
        {
            ll cnt=0;
            while(n%prime[i]==0)cnt++,n/=prime[i];
            if(i)ans*=cnt+1;
        }
        if(n>2)ans*=2;//写n>1就炸了……最后n还能等于2么 
        printf("Case %d: %lld\n",++ca,ans-1);
    }
    return 0;
}

总结

猜你喜欢

转载自blog.csdn.net/qq_41958841/article/details/82730368