码迷,mamicode.com
首页 > 其他好文 > 详细

洛谷——P1348 Couple number

时间:2017-11-17 00:11:46      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:string   题目   size   pac   col   names   read   style   str   

P1348 Couple number

题目描述

任何一个整数N都能表示成另外两个整数a和b的平方差吗?如果能,那么这个数N就叫做Couple number。你的工作就是判断一个数N是不是Couple number。

输入输出格式

输入格式:

 

仅一行,两个长整型范围内的整数n1和n2,之间用1个空格隔开。

 

输出格式:

 

输出在n1到n2范围内有多少个Couple number。

注意:包括n1和n2两个数,且n1<n2,n2 - n1 <= 10 000 000。

 

输入输出样例

输入样例#1: 复制
1 10
输出样例#1: 复制
7




打表找规律
1    1
2    1
3    2
4    3
5    4
6    4
7    5
8    6
9    7
10    7
11    8
12    9
13    10
14    10
15    11
16    12
17    13
18    13
19    14
20    15
21    16
22    16
23    17
24    18
25    19
26    19
27    20
28    21
29    22
30    22

规律:

1  1  2  3

4  4  5  6

7  7  8  9

所以s=n/4*3+n%4-1  (n%4>1‘)

  s=n/4*3+n%4     (n%4<=1)

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
int f1,f2;
LL read()
{
    LL x=0,f=1;char ch=getchar();
    while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}
    while(ch>=0&&ch<=9) x=x*10+ch-0,ch=getchar();
    return x*f;
}
LL work(LL n)
{
    if(n%4<=1) return n/4*3+n%4;
    return n/4*3+n%4-1; 
}
int main()
{
    LL a=read(),b=read(),ans;
    if(a>0) f1=1;else f1=-1;
    if(b>0) f2=1;else f2=-1;
    a=abs(a),b=abs(b);
    if(a>b) swap(a,b); 
    if(f1*f2>0) 
    {
        LL ans1=work(a-1),ans2=work(b);
        ans=abs(ans1-ans2);
    }
    else 
    {
        LL ans1=work(a),ans2=work(b);
        ans=ans1+ans2+1;
    }
    printf("%lld",ans);
    return 0;
}

 

洛谷——P1348 Couple number

标签:string   题目   size   pac   col   names   read   style   str   

原文地址:http://www.cnblogs.com/z360/p/7846659.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!