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

本文介绍了一种算法,用于求解一个给定整数N可以被表示为连续整数之和的不同方式的数量。通过将问题转化为求N的奇数因子个数,利用筛法预先计算质数,并统计每个测试用例中N的有效因子数量。

题目链接

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;
}

总结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值