UVa 10290 {Sum+=i++} to Reach N (数论-整数和素数,组合数学-排列组合)
来源:互联网 发布:农药稀释倍数通用算法 编辑:程序博客网 时间:2024/06/18 08:55
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。
当m为奇数,a+a+m-1必为偶数;当m为偶数,a+a+m-1必为奇数,所以为2n = 奇数×偶数。
需要将2n分解质因数,因奇数×奇数=奇数,所以求出2n的奇质因数的排列组合数,即所求方法数。
解题代码:
#include <iostream>#include <cstring>using namespace std;const int MAXN = 3000010;long long N, factorA[MAXN], factorB[MAXN], totFactor, prime[MAXN], totPrime, ans;bool isPrime[MAXN];void getPrime(int n) { memset(isPrime, true, sizeof(isPrime)); totPrime = 0; for (int i = 2; i <= n; i++) { if (isPrime[i]) { prime[++totPrime] = i; } for (int j = 1; j <= totPrime && i*prime[j] <= n; j++) { isPrime[i*prime[j]] = false; if (i % prime[j] == 0) break; } }}void getFactor(long long n) { totFactor = 0; long long now = n; for (int i = 1; i <= totPrime && prime[i] <= now; i++) { if (now % prime[i] == 0) { factorA[++totFactor] = prime[i]; factorB[totFactor] = 0; while (now % prime[i] == 0) { factorB[totFactor]++; now /= prime[i]; } } } if (now != 1) { factorA[++totFactor] = now; factorB[totFactor] = 1; }}void solve() { while (N & 1 ==0 ) N >>= 1; getFactor(N); ans = 1; for (int i = 1; i <= totFactor; i++) { if (factorA[i] & 1 == 1) { ans *= factorB[i] + 1; } } cout << ans << endl;}int main() { ios::sync_with_stdio(false); getPrime(3000000); while (cin >> N) { solve(); } return 0;}
- UVa 10290 {Sum+=i++} to Reach N (数论-整数和素数,组合数学-排列组合)
- uva 10290 {Sum+=i++} to Reach N (数论-整数和素数)
- uva 10290 - {Sum+=i++} to Reach N(数论-整数因子分解)
- UVa 10290 - {Sum+=i++} to Reach N
- Uva10290 - {Sum+=i++} to Reach N
- HDU 1695 GCD (数论-整数和素数,组合数学-容斥原理)
- HDU 1695 GCD (数论-整数和素数,欧拉函数,组合数学-容斥原理)
- UVA 11076 - Add Again(数论+组合数学)
- UVA 10375 - Choose and divide(数论)(组合数学)
- UVa 10892 LCM Cardinality (数论+组合数学)
- 组合数学->排列组合
- 组合数学-排列组合整理
- POJ 2154 Color(组合数学-波利亚计数,数论-欧拉函数,整数快速幂)
- HDU 3240 Counting Binary Trees(组合数学-卡特兰数,数论-整数快速幂,数论-求逆元)
- 【UVA 11426】 【求sum(gcd(i,j),1<=i<j<=n)1<n<4000001】
- bzoj 2142 礼物 (组合数学+数论)
- 数学训练----数论Sum
- 算法_3 : 组合数学:排列组合
- poj 1836 Alignment DP
- [Phonegap+Sencha Touch] 移动开发28 小米3不支持Sencha touch的解决办法
- wghegbew
- 【eclipse插件开发实战】Eclipse插件开发4——插件JDE、PDE开发方式及plugin.xml配置文件结构
- POJ 3404 Bridge over a rough river(数学推理题)
- UVa 10290 {Sum+=i++} to Reach N (数论-整数和素数,组合数学-排列组合)
- [Phonegap+Sencha Touch] 移动开发29 安卓navigator.camera.getPicture得到图片的真实路径
- SIFT和SURF,及OpenCV实现
- IO流
- hdu 1087 Super Jumping! Jumping! Jumping!(思想题)
- myeclipse10+spring mvc+ibatis搭建的一个demo
- AzCopy – 上传/下载 Windows Azure Blob 文件
- HDU1394--Minimum Inversion Number
- jQuery formValidator表单验证插件