Poj 2853,2140 Sequence Sum Possibilities(因式分解)
一、Description
Most positive integers may be written as a sum of a sequence of at least two consecutive positive integers. For instance,
6 = 1 + 2 + 3
9 = 5 + 4 = 2 + 3 + 4
but 8 cannot be so written.
Write a program which will compute how many different ways an input number may be written as a sum of a sequence of at least two consecutive positive integers.
Input
The first line of input will contain the number of problem instances N on a line by itself, (1
≤ N ≤ 1000) . This will be follo
wed by N lines, one for each problem instance. Each problem line will have the problem number, a single space and the number to be written as a sequence of consecutive positive integers. The second number will be less than 231 (so will fit
in a 32-bit integer).
Output
The output for each problem instance will be a single line containing the problem number, a single space and the number of ways the input number can be written as a sequence of consecutive positive integers.
二、题解
一种解法:
1. 这串连续的整数可以表示为:(x+1)+(x+2)+(x+3)+(x+4)+……+(x+i) , 其中x=0,1,2 ......
2. 假设已经找到:(x+1)+(x+2)+(x+3)+(x+4)+……+(x+i) =number;
3. A=number-(1+2+3+......+i)=number-(1+i)*i/2;
4. A=x*i;即A一定能被i整除,从而达到判定目的.
三、java代码
import java.util.Scanner; public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int i,j,n,num,sum,temp;
n=sc.nextInt();
for(i=0;i<n;i++){
num=sc.nextInt();
sum=0;
temp=sc.nextInt();
for(j=2;(j+1)*j/2<=temp;j++){
if((temp-(j+1)*j/2)%j==0)
sum++;
}
System.out.println(num+" "+(sum));
}
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
Poj 2853,2140 Sequence Sum Possibilities(因式分解)的更多相关文章
- POJ 2853 Sequence Sum Possibilities
Sequence Sum Possibilities Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5537 Accep ...
- poj 1699 Best Sequence(AC自己主动机+如压力DP)
id=1699" target="_blank" style="">题目链接:poj 1699 Best Sequence 题目大意:给定N个D ...
- [poj P1141] Brackets Sequence
[poj P1141] Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Special Judge Description ...
- Poj 1019 Number Sequence( 数据分析和操作)
一.题目大意 有这样一个序列包含S1,S2,S3...SK,每一个Si包括整数1到 i.求在这个序列中给定的整数n为下标的数. 例如,前80位为1121231234123451234561234567 ...
- Ural 1248 Sequence Sum 题解
目录 Ural 1248 Sequence Sum 题解 题意 题解 程序 Ural 1248 Sequence Sum 题解 题意 给定\(n\)个用科学计数法表示的实数\((10^{-100}\s ...
- POJ 2479 Maximum sum POJ 2593 Max Sequence
d(A) = max{sum(a[s1]..a[t1]) + sum(a[s2]..a[t2]) | 1<=s1<=t1<s2<=t2<=n} 即求两个子序列和的和的最大 ...
- POJ 2478 Farey Sequence
名字是法雷数列其实是欧拉phi函数 Farey Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- poj 2478 Farey Sequence 欧拉函数前缀和
Farey Sequence Time Limit: 1000MS Memory Limit: 65536K Description The Farey Sequence Fn for ...
- poj 1019 Number Sequence 【组合数学+数字x的位宽函数】
题目地址:http://poj.org/problem?id=1019 Number Sequence Time Limit: 1000MS Memory Limit: 10000K Total ...
随机推荐
- 9.接口BeanPostProcessor
package org.springframework.beans.factory.config; import org.springframework.beans.BeansException; p ...
- php在不同平台下路径分隔符不同的解决办法
在看phpamf时看到一个常量“DIRECTORY_SEPARATOR”,最后发现是一个全局的常量,用来定义路径分隔符的 主要解决在windows和linux下路径分隔符不同的造成代码不通用的问题,在 ...
- Delphi 7里Messages.pas里所有104种重定义消息种类,180种不同的消息名称
Delphi 7里Messages.pas里所有消息.经统计,共104种重定义消息种类,方便使用,180种不同的消息名称.省得像VC里一样,处处自己解析wParam和LParam参数进行分析.有空我要 ...
- python爬虫之request and BeautifulSoup
1.爬虫的本质是什么? 模仿浏览器的行为,爬取网页信息. 2.requests 1.get请求 无参数实例 import requests ret = requests.get('https://gi ...
- Bootstrap学习5--bootstrap中的模态框(modal,弹出层)
bootstrap中的模态框(modal),不同于Tooltips,模态框以弹出对话框的形式出现,具有最小和最实用的功能集. 务必将模态框的 HTML 代码放在文档的最高层级内(也就是说,尽量作为 b ...
- Android Studio support 26.0.0-alpha1 Failed to resolve: com.android.support:appcompat-v7:27.+ 报错解决方法
AS下如何生成自定义的.jks签名文件, 以及如何生成数字签名 链接:http://www.cnblogs.com/smyhvae/p/4456420.html 链接:http://blog.csdn ...
- sublime 添加 颜色插件 colorcoder
高亮所有变量,因此可以极大的简化代码定位.尤其是对那些有阅读障碍的程序员非常有帮助.
- 数据库抽象层PDO
通过数据库抽象层PDO可以访问多个数据库 //数据库抽象层PDO //造DSN:驱动名:dbname=数据库名:host=服务器地址 $dsn = "mysql:dbname=mydb;ho ...
- 培训笔记——Linux目录说明
一般我们的电脑里都只有一块硬盘,但是这块硬盘怎么使用呢? 我们的头脑里大体有个分区的概念,为什么要分区呢? 不是很清楚,不过有句话说 不要把鸡蛋放在同一个篮子里,可能有这种考虑吧. 好,最起码知道分区 ...
- Android开发--多线程之Handler
前言 Android的消息传递机制是另外一种形式的“事件处理”,这种机制主要是为了解决Android应用中多线程的问题,在Android中不 允许Activity新启动的线程访问该Activity里的 ...