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>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N=3e5+20;
//
string s;
ll n;
bool check(ll x,ll y)
{
	return (x+y)*(y-x+1)>=2*n;
}
int main()
{
	while(cin>>n)
	{
		int cnt=0;
		n=n*2;
		for(int i=1;i*i<=n;i++)
		{
			int x=n/i;
			if(n%i==0)
			{
				if(x%2||i%2)//2*n 一个因数为奇 另外一个肯定为偶数
					cnt++;
			}
		}
		cout<<cnt<<endl;
	}

	return 0;
}

  

时间: 2024-10-25 09:55:04

POJ 2140 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(等差数列)

题目链接: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

POJ 3086 Triangular Sums

Triangular Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6371   Accepted: 4529 Description The nth Triangular number, T(n) = 1 + - + n, is the sum of the first n integers. It is the number of points in a triangular array with n po

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

POJ 2140

等差公式求和:(i*2+m)*(m+1)=2*n 枚举m+1,符合公式即可. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string.h> #include <queue> #include <cmath> #include <vect

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

转载: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

【转】对于杭电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