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

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

时间: 2024-10-13 16:13:14

uva 10290 {Sum+=i++} to Reach N (数论-整数和素数)的相关文章

UVa 10290 - {Sum+=i++} to Reach N

题目:给你一个数字问将他写成连续的数字的和的形式,有几种写法. 分析:数论.设拆成的序列个数为k,我们分两种情况讨论: 1.拆成奇数个连续数,那么设中位数是a,则有n = k * a: 2.拆成偶数个连续数,那么设中位数是a与a+1,则有n = k / 2 *(a+a+1): 综上所述,本问题就是将n拆成2个数的乘积的形式,且其中一个一定为奇数: 问题转化成求解n中奇数因子的个数,这里求出所有的奇素因子计算组合数即可. 说明:实际的数据规模没有题面上那么大╮(╯▽╰)╭. #include <a

HDU 1695 GCD (数论-整数和素数,组合数学-容斥原理)

GCD Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y) = k. GCD(x, y) means the greatest common divisor of x and y. Since the number of choices may be very large, you're only required to output t

HDU 3988 Harry Potter and the Hide Story(数论-整数和素数)

Harry Potter and the Hide Story Problem Description iSea is tired of writing the story of Harry Potter, so, lucky you, solving the following problem is enough. Input The first line contains a single integer T, indicating the number of test cases. Eac

UVA 1210 Sum of Consecutive Prime Numbers(数论)

UVA - 1210 Sum of Consecutive Prime Numbers Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such

UVA 10773 Back to Intermediate Math(数论)

题目链接:Back to Intermediate Math 题意:两种过河方式,一种笔直过河,一种最快过河,求两种时间差 只要计算出两种时间,笔直过河的速度等于两个速度分量的合速度,最快就等于船速度,求出差即可. 代码: #include <stdio.h> #include <string.h> #include <math.h> int t, d, v, u; int main() { int cas = 0; scanf("%d", &

UVA 10168 Summation of Four Primes(数论)

Summation of Four Primes Input: standard input Output: standard output Time Limit: 4 seconds Euler proved in one of his classic theorems that prime numbers are infinite in number. But can every number be expressed as a summation of four positive prim

uva 10168 Summation of Four Primes(数论-哥德巴赫猜想)

Problem A Summation of Four Primes Input: standard input Output: standard output Time Limit: 4 seconds Euler proved in one of his classic theorems that prime numbers are infinite in number. But can every number be expressed as a summation of four pos

Uva10290 - {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

UVA 10581 - Partitioning for fun and profit(数论递推)

10581 - Partitioning for fun and profit 题目链接 题意:给定m, n,表示分配给n个格子,分配m个数字进去,每个格子最少1,并且序列要是递增的,问第k个字典序的序列是什么 思路:先利用dp打出表,dp[i][j][k]表示第i个数,尾巴为j,总和剩下k的情况,写一个记忆化求出,之后在这个数组基础上,从左往右枚举要放那个数字合适,合适的就放进去并且输出,注意最后一个数字要单独输出. 代码: #include <cstdio> #include <cs