Sum of Consecutive Integers LightOJ - 1278————(算术基本定理+容斥)

Given an integer N 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 N = 15 has three solutions, (1+2+3+4+5),(4+5+6),(7+8). ( 1 + 2 + 3 + 4 + 5 ) , ( 4 + 5 + 6 ) , ( 7 + 8 ) .

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

Each case starts with a line containing an integer N(1N1014). N ( 1 ≤ N ≤ 10 14 ) .

Output
For each case, print the case number and the number of ways to express N 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   N ,用一段连续正整数的和表示 N N <script type="math/tex" id="MathJax-Element-6317">N</script>的方法有多少种?

也就是找奇数因子的个数


#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using  namespace std;
typedef long long LL;
const int maxn = 10000000, INF = 0x7fffffff;
int primes[1000000];
bool vis[maxn];
int ans = 0;
void init()
{
    memset(vis,0,sizeof(vis));
    for(int i=2; i<maxn; i++)
    {
        if(vis[i]) continue;
        primes[ans++] = i;
        for(LL j=(LL)i*i; j<maxn; j+=i)
            vis[j] = 1;
    }
}
int main()
{
    init();
    int t;
    scanf("%d",&t);
    int kase=0;
    while(t--)
    {
         LL n, res = 1;
        cin>> n;
        for(int i=0; i<ans && primes[i]*primes[i] <= n; i++)
        {
            LL cnt2 = 0;
            while(n % primes[i] == 0)
            {
                n /= primes[i];
                cnt2++;
            }
            if(cnt2 > 0 && primes[i] % 2)
                res *= (cnt2 + 1);
        }
        if(n > 1 && n % 2)
        {
            res *= 2;
        }
        printf("Case %d: %lld\n", ++kase, res - 1);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值