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 ...
随机推荐
- Sql注入_类型
1.sql注入 通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令. 2.sql注入类型 按照注入点类型来分类 (1)数字型注入点 在 Web ...
- 【android】在Service的onStartCommand()中调用stopself()应该注意的问题
在Service的onStartCommand()中调用stopself()后并不会立马destroy掉service,而是等onStartCommand()运行完才destroy. public c ...
- python学习-4-类的使用
class Animal: def __init__(self, name): # Constructor of the class self.name = name def talk(self): ...
- python学习-1-列表,元组,字典,集合,文件操作
1. 列表.元组操作 names = ['Alex',"Tenglan",'Eric'] >>> names[0] >>> names[2] & ...
- Python赋值原理:Python无变量,万物皆对象
有几个和以前的常见语言,比如c语言不同 改变变量数据不覆盖原来的 name = '苍老师' print(id(name)) name = '志玲' print(id(name)) 运行结果 73955 ...
- rewrite_static
<?php class MyObject { public static $myStaticVar = 0; function myMethod() { self::$myStaticVar + ...
- iOS UIImage 拉伸问题 (适用于UIButton等需要局部拉伸的情况)
图片 有的切图切很大 还占用ipa大小,有时候 切图 只需要 局部或者说 一个压缩的图片的抽象状态 直接上代码 CGFloat top = ; // 顶端盖高度 ; // 底端盖高度 ; // 左端盖 ...
- java程序实现Unicode码和中文互相转换
根据前一篇的补充问题http://blog.csdn.net/fancylovejava/article/details/10142391 有了前一篇文章的了解,大概了解了unicode编码格式了 ...
- dev系列之gridview
gridview新增一行就激活编辑,及显示闪动的光标 gridView1.ShowEditor(); 隐藏Gridview表头上面的panel this.gridView1.OptionsView.S ...
- Luogu-4049 [JSOI2007]合金
题目中给出了三种金属的比例,实际上只用考虑两个就可以,第三个可以由另外两个确定qwq 如果把原料和需求看做二维平面上的点,可以发现两种原料能混合成的比例就在他们相连的线段上,也就是说线段上的点都能混合 ...