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. cocos2d-x 多分辨率适配详解(转载),以前北京团队设计的游戏,也是用这套方案

    http://blog.csdn.net/kyo7552/article/details/17163487 多种分辨率的适配一直都是一个蛋疼的问题,各家公司可能都有自己的一套方案.今天我为大家介绍的是 ...

  2. Microsoft SQL Server 2012 数据库安装图解教程

    本文部分引用以下文章: SQL Server 2012 安装图解教程(附sql2012下载地址)_MsSql_脚本之家 http://www.jb51.net/article/36049.htm SQ ...

  3. P107、面试题15:链表中倒数第K个结点

    题目:输入一个链表,输出该链表中倒数第K个结点.为了符合大多数人的习惯,本体从1开始奇数,即链表的尾结点是倒数第1个结点.例如一个链表有6个结点,从头结点开始他们的值一次是1.2.3.4.5.6.这个 ...

  4. word文档左侧显示目录

    word2007  选择word的视图,然后选择文档结构图

  5. poj 2635 The Embarrassed Cryptographer(数论)

    题目:http://poj.org/problem?id=2635 高精度求模  同余模定理. 题意: 给定一个大数K,K是两个大素数的乘积的值.再给定一个int内的数L 问这两个大素数中最小的一个是 ...

  6. sharedevelop iis express

    sharedevelop 的IIS express的配置文件在 %userprofile%\documents\IISExpress\config\applicationhost.config 自动会 ...

  7. 设计模式 - interpreter

    interpreter模式在GOF中的定义是:给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子. 定义看起来是非常晦涩.简单来讲,解释器模式通常用于处理特 ...

  8. poj3249

    显然是一道最短路径的题目,但是 1 ≤ n ≤ 100000, 0 ≤ m ≤ 1000000能轻松打爆dij+heap 怎么办? 挖掘题意,这是一个DAG图(有向无环图) 所以对于此类问题,我们有特 ...

  9. 解锁windowsphone设备遇到的错误:检查Miscrosoft账户凭据、请重新注册 0x80004005 解决方案

    本篇文章主要讲在解锁windowsphone设备时遇到的错误 Error1:登录windowsphone开发人员中心时出错,请检查您的miscrosoft账户凭据 Error2:注册您的手机时出现未知 ...

  10. UVa 1645 (递推) Count

    题意: 有多少个n个节点的有根树,满足每层节点的子节点个数相同,输出该数目除以1e9+7的余数. 分析: 这种题目就属于那种,看起来很高冷,读完题更高冷.想了N久想不出来,一搜题解,卧槽,这么sb的题 ...