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

  

hdu 2715 Herd Sums的更多相关文章

  1. POJ 2140 Herd Sums

    http://poj.org/problem?id=2140 Description The cows in farmer John's herd are numbered and branded w ...

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

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

  3. POJ解题经验交流

    感谢范意凯.陈申奥.庞可.杭业晟.王飞飏.周俊豪.沈逸轩等同学的收集整理.   题号:1003 Hangover求1/2+1/3+...1/n的和,问需多少项的和能超过给定的值 类似于Zerojudg ...

  4. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  5. 算法之路 level 01 problem set

    2992.357000 1000 A+B Problem1214.840000 1002 487-32791070.603000 1004 Financial Management880.192000 ...

  6. 【转】POJ百道水题列表

    以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...

  7. hdu 4193 Non-negative Partial Sums 单调队列。

    Non-negative Partial Sums Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  8. hdu 4193 Non-negative Partial Sums

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4193 题意:给出一个n数列,要求把前i(1<=i<=n)个数移到剩余数列的后面形成新的数列 ...

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

    HDU 4193 题意:给n个数字组成的序列(n <= 10^6).求该序列的循环同构序列中,有多少个序列的随意前i项和均大于或等于0. 思路: 这题看到数据规模认为仅仅能用最多O(nlogn) ...

随机推荐

  1. Java 垃圾回收机制

    1.delete是C++里面用于释放内存的运算符,而不是Java. 2.当发现某个对象的引用计数为0时,就将对象列入待回收列表中,并不是马上予以销毁. 3.System.gc()仅仅是一个回收请求,J ...

  2. 文件结束符和C\C++读取文件方式

    http://www.cnblogs.com/cvbnm/articles/2003056.html 约定编译器为 gcc2/x86: 所以 char, unsigned char 为 8 位, in ...

  3. java,<E>什么意思?

    泛型 就是指任意类型 比如 HashMap<K,V> 你用的时候 你可以将K,V 设置成任意类 HashMap<String,Integer> K,V 和那个E 一样 Hash ...

  4. 关于 hot code replace fail 问题 .

    频频出现Hot code replace failed问题.网上查不到解决方法,想来想去,是否是jvm的问题?我的jre使用自己下载的jdk1.6.07,而MyEclipse的jvm自带的是1.5.0 ...

  5. No ongoing transaction. Did you forget to call multi?

    2016-10-21 14:41:47,551 [ERROR] [http-nio-8032-exec-2] TransactionSynchronizationUtils:171 - Transac ...

  6. PostgreSQL和MySQL like区别

    前言:今天在PostgreSQL中使用like,字段类型是int,执行语句报错, 1.表结构:都是用sysbench工具产生的 postgres=# \d sbtest1;               ...

  7. Lambda表达式 =>(msdn)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. 在XML里的XSD和DTD以及standalone的使用

    有关XML结构中的XSD和DTD以及standalone的使用 XmlDeclaration declare= document.CreateXmlDeclaration("1.0" ...

  9. JavaScript Function.apply() 函数详解

    apply()函数用于调用当前函数functionObject,并可同时使用指定对象thisObj作为本次函数执行时函数内部的this指针引用. 该函数属于Function对象,所有主流浏览器均支持该 ...

  10. 如何写mysql的定时任务

     什么是事件: 一组SQL集,用来执行定时任务,跟触发器很像,都是被动执行的,事件是因为时间到了触发执行,而触发器是因为某件事件(增删改)触发执行: 查看是否开启: show variables li ...