一、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. ubuntu 安装wine

    笔记 1.安装源 sudo add-apt-repository ppa:wine/wine-builds sudo apt-get update 2.安装wine sudo apt-get inst ...

  2. 电路分析三------KCL,KVL,VCR方程

    1.2b方程 2.举例 举例2

  3. Java 重写 equals 与 hashCode 的注意事项

    为什么重写 equals 的时候必须重写 hashCode 大家可能从很多教程中了解到: SUN官方的文档中规定"如果重定义equals方法,就必须重定义hashCode方法,以便用户可以将 ...

  4. 文件传输协议FTP

    之前已经了解了TCP/IP这种低级别的协议,还有一些网络协议包括文件传输(FTP,STP).阅读Usenet新闻组(NNTP).电子邮件发送(SMTP).从服务器上下载电子邮件(POP3.IMAP)等 ...

  5. 一对多 添加表单 cocoon

    gem 'cocoon' - javascript "cocoon.js" https://note.youdao.com/web/#/file/XCiivnE/note/WEB4 ...

  6. 数组元素的删除 【vector】

    7-5 数组元素的删除(5 分) 完成数组元素的移动功能:假设数组有n个元素,输入一个数x,把数组的第x个位置的元素删除了,后面的元素依次前进一个位置. 重复若干次这样的删除,得到最后的结果. 输入格 ...

  7. Thread.currentThread().getContextClassLoader() and Class.getClassLoader()

    Thread.currentThread().getContextClassLoader() and Class.getClassLoader()   一.同一工程中: String path = T ...

  8. pygame躲敌人的游戏

    #first.py# coding=utf- import pygame from pygame.locals import * from sys import exit from util impo ...

  9. [原创]java WEB学习笔记09:ServletResponse & HttpServletResponse

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  10. poj 1065 Wooden Sticks 【贪心 新思维】

    题目地址:http://poj.org/problem?id=1065 Sample Input 3 5 4 9 5 2 2 1 3 5 1 4 3 2 2 1 1 2 2 3 1 3 2 2 3 1 ...