hdu 2715 Herd Sums

Herd Sums

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 772    Accepted Submission(s): 375

Problem Description

The cows in farmer John‘s herd are numbered and branded with consecutive integers from 1 to N (1 <= N <= 10,000,000). When the cows come to the barn for milking, they always come in sequential order from 1 to N.

Farmer John, who majored in mathematics in college and loves numbers, often looks for patterns. He has noticed that when he has exactly 15 cows in his herd, there are precisely four ways that the numbers on any set of one or more consecutive cows can add up to 15 (the same as the total number of cows). They are: 15, 7+8, 4+5+6, and 1+2+3+4+5.

When the number of cows in the herd is 10, the number of ways he can sum consecutive cows and get 10 drops to 2: namely 1+2+3+4 and 10.

Write a program that will compute the number of ways farmer John can sum the numbers on consecutive cows to equal N. Do not use precomputation to solve this problem.

Input

* Line 1: A single integer: N

Output

* Line 1: A single integer that is the number of ways consecutive cow brands can sum to N.

Sample Input

15

Sample Output

4

根据等差数列求和公式S=(a1+an)*n/2和末项公式an=a1+(n-1)*d(d位公差)得a1=(2*s+n-n*n)/2/n;得出求a1的公式然后对所有的n(n为项数)进行枚举,得出结果

2*s=(2*a1+n-1)*n所以n为偶数或者(2*a1+n-1)为偶数且a1不等于0

#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
	int n,m,j,i;
	while(scanf("%d",&n)!=EOF)
	{
		int ans=sqrt(2*n);
		int ant=0;
		for(i=1;i<=ans;i++)
		{
			m=(2*n+i-i*i)/2/i;
			if(2*m*i+i*i-i==2*n&&m>0&&(i%2==0||(2*m+i-1)%2==0))
			    ant++;
		}
		printf("%d\n",ant);
	}
	return 0;
}

  

时间: 2024-10-10 17:09:37

hdu 2715 Herd Sums的相关文章

POJ 2140 Herd Sums

http://poj.org/problem?id=2140 Description The cows in farmer John's herd are numbered and branded with consecutive integers from 1 to N (1 <= N <= 10,000,000).  When the cows come to the barn for milking, they always come in sequential order from 1

POJ 2140 Herd Sums 公式推导

题意:给出n<=1e7 求有多少个连续数之和等于k x+x+1+....x+k=n (k+1)k/2+(k+1)x=n (k+1)k+(k+1)2x=2*n (k+1)*(2x+k)=2*n    2*n为偶 k+1,2x+k都为2*n因子 &&一奇一偶 得到:2*n有多少个奇因子(或者偶因子)就有多少对解 (k,n固定可以得到首项x 得出一解) #include <iostream> #include <cstdio> #include <cmath&

HDU Non-negative Partial Sums (单调队列)

Problem Description You are given a sequence of n numbers a0,..., an-1. A cyclic shift by k positions (0<=k<=n-1) results in the following sequence: ak ak+1,..., an-1, a0, a1,..., ak-1. How many of the n cyclic shifts satisfy the condition that the

poj 2140 Herd Sums(等差数列)

题目链接:http://poj.org/problem?id=2140 题意:给出n,求一共有多少个连续的数满足加和恰好得到n, 思路:这题想了好久一开始以为是打表找规律,后来才发现与等差数列有关...(这题也可以用DP来做) 根据等差数列求和公式S=(a1+an)*n/2和末项公式an=a1+(n-1)*d(d位公差)得a1=(2*s+n-n*n)/2/n;得出求a1的公式然后对所有的n(n为项数)进行枚举,得出结果 2*s=(2*a1+n-1)*n,因为2*S必为偶数所以n为偶数或者(2*a

转载:hdu 题目分类 (侵删)

转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093. 1094.1095.1096.1097.1098.1106.1108.1157.116

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

【转】对于杭电OJ题目的分类

[好像博客园不能直接转载,所以我复制过来了..] 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDI

HDU 4193 Non-negative Partial Sums(想法题,单调队列)

HDU 4193 题意:给n个数字组成的序列(n <= 10^6),求该序列的循环同构序列中,有多少个序列的任意前i项和均大于或等于0. 思路: 这题看到数据规模觉得只能用最多O(nlogn)的算法,然后想到了之前刚做过的有关最小表示法的题,但还没证明出一个做这题有效的算法出来. 后来看过题解,发现用的最多的方法是单调队列,然而我对这个知识点知之甚少orz /*科普君:from单调队列 单调队列是指:队列中元素之间的关系具有单调性,而且,队首和队尾都可以进行出队操作,只有队尾可以进行入队操作.