一、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(因式分解)的更多相关文章

  1. POJ 2853 Sequence Sum Possibilities

    Sequence Sum Possibilities Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5537   Accep ...

  2. poj 1699 Best Sequence(AC自己主动机+如压力DP)

    id=1699" target="_blank" style="">题目链接:poj 1699 Best Sequence 题目大意:给定N个D ...

  3. [poj P1141] Brackets Sequence

    [poj P1141] Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K   Special Judge Description ...

  4. Poj 1019 Number Sequence( 数据分析和操作)

    一.题目大意 有这样一个序列包含S1,S2,S3...SK,每一个Si包括整数1到 i.求在这个序列中给定的整数n为下标的数. 例如,前80位为1121231234123451234561234567 ...

  5. Ural 1248 Sequence Sum 题解

    目录 Ural 1248 Sequence Sum 题解 题意 题解 程序 Ural 1248 Sequence Sum 题解 题意 给定\(n\)个用科学计数法表示的实数\((10^{-100}\s ...

  6. 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} 即求两个子序列和的和的最大 ...

  7. POJ 2478 Farey Sequence

     名字是法雷数列其实是欧拉phi函数              Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  8. poj 2478 Farey Sequence 欧拉函数前缀和

    Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K       Description The Farey Sequence Fn for ...

  9. poj 1019 Number Sequence 【组合数学+数字x的位宽函数】

    题目地址:http://poj.org/problem?id=1019 Number Sequence Time Limit: 1000MS   Memory Limit: 10000K Total ...

随机推荐

  1. 九度OJ 1324:The Best Rank(最优排名) (排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:489 解决:126 题目描述: To evaluate the performance of our first year CS major ...

  2. 6.1-AliasRegistry

    AliasRegistry //AliasRegistry package org.springframework.core; /** * Common interface for managing ...

  3. 浅谈命令查询职责分离(CQRS)模式---转载

    在常用的三层架构中,通常都是通过数据访问层来修改或者查询数据,一般修改和查询使用的是相同的实体.在一些业务逻辑简单的系统中可能没有什么问题,但是随着系统逻辑变得复杂,用户增多,这种设计就会出现一些性能 ...

  4. openstack 官方镜像qcow2 下载和修改密码

    下载地址: CentOS6:http://cloud.centos.org/centos/6/images/ CentOS7:http://cloud.centos.org/centos/7/imag ...

  5. mysql 分页测试,

    大环境:MySQL5.6 自己造了 27万数据, 一次性 查出来,会超时: 而分页跑,会查出来8s: 但是在少于27万时,直接查比 分页查快5倍:

  6. QT5的exe的发布

    直接release的exe文件需要很多dll关联,一个一个找又太麻烦. 其实QT5带有一个 windeployqt 工具 Qt Widgets Application可执行程序发布方式 首先用 QtC ...

  7. vim下的ctags和taglist等的使用和配置

    1.ctags (1)到 http://prdownloads.sourceforge.net/ctags/ctags-5.6.tar.gz         下载ctags源码ctags-5.6.ta ...

  8. 机器学习Coursera学习总结

    Coursera上Andrew NG的机器学习实在是太火了,最近有时间花费了20来天的时间(每天3小时左右)终于学习完了全部的课程,总结如下: (1)适合入门,讲的比较基础,Andrew讲的很棒: ( ...

  9. Data Structure Binary Tree: Largest Independent Set Problem

    http://www.geeksforgeeks.org/largest-independent-set-problem/ #include <iostream> #include < ...

  10. Linuxshell资料汇总

    1.判断文件是否存在 https://www.cnblogs.com/platero/p/4021561.html 2.日期赋值 https://www.cnblogs.com/lonelywolfm ...