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+4, 4+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 (数论-整数和素数)