1. 程式人生 > >【題解】LightOJ1278 Sum of Consecutive Integers 線性篩

【題解】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; }

總結