码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-07-31 20:49:07      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:style   blog   os   strong   io   for   2014   ar   

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





uva 10290 {Sum+=i++} to Reach N (数论-整数和素数),布布扣,bubuko.com

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

标签:style   blog   os   strong   io   for   2014   ar   

原文地址:http://blog.csdn.net/a1061747415/article/details/38322329

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!