uva 10290 {Sum+=i++} to Reach N (数论-整数和素数)

Problem H

{sum+=i++} to Reach N

Input: standard input

Output:  standard output

Memory Limit: 32 MB

 

All the positive numbers can be expressed as a sum of one, two or more consecutive positive integers. For example 9 can be expressed in three such ways, 2+3+44+5 or 9. Given an integer less than (9*10^14+1) or (9E14 + 1) or (9*1014 +1) you will have to determine in how many ways that number can be expressed as summation of consecutive numbers.

 

Input

The input file contains less than 1100 lines of input. Each line contains a single integer N  (0<=N<= 9E14). Input is terminated by end of file.

 

Output

For each line of input produce one line of output. This line contains an integer which tells in how many ways N can be expressed as summation of consecutive integers.

 

Sample Input

9

11

12

 

Sample Output

3

2

2


(Math Lovers’ Contest, Problem Setter: Shahriar Manzoor)


题目大意:

问一个数n用连续的几个数相加表示的方案数。


解题思路:

假设首项为a,有m项,则 (a+a+m-1)*m=2*n,所以为奇数*偶数的结果,只需要算出2*n用奇数表示的方法数即可。


解题代码:

#include <iostream>
#include <cstdio>
#include <map>
#include <vector>
#include <cstring>
using namespace std;

typedef long long ll;

const int maxn=30000010;
bool isPrime[maxn];
vector <ll> v;
ll tol;

void get_prime(){
    tol=0;
    memset(isPrime,true,sizeof(isPrime));
    for(ll i=2;i<maxn;i++){
        if(isPrime[i]){
            tol++;
            v.push_back(i);
        }
        for(ll j=0;j<tol && i*v[j]<maxn;j++){
            isPrime[i*v[j]]=false;
            if(i%v[j]==0) break;
        }
    }
    //for(ll i=0;i<20;i++) cout<<v[i]<<endl;
}

inline map <ll,ll> getPrime(ll x){
    map <ll,ll> mp;
    for(ll i=0;i<tol && x>=v[i]*v[i];i++){
        while(x>0 && x%v[i]==0){
            x/=v[i];
            mp[v[i]]++;
        }
    }
    if(x>1) mp[x]++;
    return mp;
}

int main(){
    get_prime();
    ll x;
    while(cin>>x){
        ll sum=1;
        map <ll,ll> mp=getPrime(2*x);
        for(map <ll,ll>::iterator it=mp.begin();it!=mp.end();it++){
            //cout<<it->first<<" "<<it->second<<endl;
            if((it->first)&1) sum*=(it->second+1);
        }
        cout<<sum<<endl;
    }
    return 0;
}





转载于:https://www.cnblogs.com/toyking/p/3893153.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值