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
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.
根据等差数列求和公式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的更多相关文章
- POJ 2140 Herd Sums
http://poj.org/problem?id=2140 Description The cows in farmer John's herd are numbered and branded w ...
- 转载:hdu 题目分类 (侵删)
转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...
- POJ解题经验交流
感谢范意凯.陈申奥.庞可.杭业晟.王飞飏.周俊豪.沈逸轩等同学的收集整理. 题号:1003 Hangover求1/2+1/3+...1/n的和,问需多少项的和能超过给定的值 类似于Zerojudg ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
- 算法之路 level 01 problem set
2992.357000 1000 A+B Problem1214.840000 1002 487-32791070.603000 1004 Financial Management880.192000 ...
- 【转】POJ百道水题列表
以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...
- hdu 4193 Non-negative Partial Sums 单调队列。
Non-negative Partial Sums Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- hdu 4193 Non-negative Partial Sums
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4193 题意:给出一个n数列,要求把前i(1<=i<=n)个数移到剩余数列的后面形成新的数列 ...
- HDU 4193 Non-negative Partial Sums(想法题,单调队列)
HDU 4193 题意:给n个数字组成的序列(n <= 10^6).求该序列的循环同构序列中,有多少个序列的随意前i项和均大于或等于0. 思路: 这题看到数据规模认为仅仅能用最多O(nlogn) ...
随机推荐
- CyanogenMod刷机以及Google Play应用商店安装方法介绍
http://blog.csdn.net/zcynical/article/details/19241595 写在前面: 本文介绍的方法除第一步外,适用于所有CM系统支持的设备,第一步由于用到了PC上 ...
- arcgis 10.2 安装教程
arcgis 10.2 安装教程(含下载地址)_百度经验 http://jingyan.baidu.com/article/fc07f98911b66912ffe5199b.html arcgis 1 ...
- 188. Best Time to Buy and Sell Stock IV
题目: 链接: 题解: 测试: Reference:
- nyist 510昂贵的聘礼
/* 好好的图论题啊,最短路的应用,dijkstra算法 */ #include <iostream> using namespace std; const int INF=100000; ...
- WinAPI——钩子函数大全2
CallNextHookEx 函数功能:该函数发送挂钩信息给当前挂钩链中的下一个挂钩处理过程,一个挂钩处理过程可在对该挂钩信息进行处理之前或之后调用本函数. 函数原形:LRESULT CallNext ...
- poj 1924 Paths on a Grid(组合数学)
题目:http://poj.org/problem?id=1942 题意:给定一个矩形网格的长m和高n,其中m和n都是unsigned int32类型,一格代表一个单位,就是一步,求从左下角到右上角有 ...
- 8.3-8.7 usaco
summary:38 vijos1002:青蛙跳河. dp+压缩.距离大于100可以直接%100.然后数据范围小了很多可以dp了. #include<cstdio> #include< ...
- 我的电脑在用Microsoft Script Editor 调试,关不了?
是不是上网后经常出现错误提示框,那在浏览器的工具选“internet选项”进入“高级”在“禁用脚本调试(internet explor)”和“禁用脚本调试”前勾上.Microsoft Script E ...
- linux tmp75 /dev/i2c-* 获取数据 demo
/********************************************************************** * linux tmp75 /dev/i2c-* 获取数 ...
- datagrid中需要填写长文本,扩展的textarea
$.extend($.fn.datagrid.defaults.editors, { textarea: {//textarea就是你要自定义editor的名称 init: function(cont ...