zoj3629 Treasure Hunt IV

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3629

思路:找规律,发现符合要求的数为

[0,1)

[4,9)

[16,25)

[36,49)

…………

[n^2 , (n+1)^2)

发现 n^2 到(n+1)^2(n为偶数)前开后闭的区间为符合要求的数,然后发现(n+1)*(n+1)-n*n组成的数列为一个差值为4等差数列,我们需要求区间[a,b]符合要求的数,那么只需要用b前面符合要求的数减去a-1中符合要求的数。。。。。

开始的时候一直WA,到后才发现输入输出时用的%I64d要换成%lld,悲剧呀。。。。。。

code:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;

typedef long long LL;

LL f(LL x)   //计算0到x之间符合要求的数,等差数列首项看为1
{
    if(x==-1) return 0;
    LL a=sqrt(x);
    LL sum=0;
    if(a*a==x)
    {
        LL n=(a+1)/2;
        sum=n+n*(n-1)*2;
        if(a%2==0)
        {
            sum++;
        }
    }
    else if(a*a<x)
    {
        if(a%2==0)
        {
            LL n=a/2;
            sum=n+n*(n-1)*2;
            sum+=(x-a*a+1);
        }
        if(a%2==1)
        {
            LL n=(a+1)/2;
            sum=n+n*(n-1)*2;
        }
    }
    return sum;
}

int main()
{
    long long  a,b,m,n,i;
    while(scanf("%lld%lld",&a,&b)==2)
    {

        printf("%lld\n",f(b)-f(a-1));
        //cout<<f(b)-f(a-1)<<endl;
    }
    return 0;
}
时间: 2024-08-22 05:44:32

zoj3629 Treasure Hunt IV的相关文章

ZOJ3629 Treasure Hunt IV(找规律,推公式)

Treasure Hunt IV Time Limit: 2 Seconds      Memory Limit: 65536 KB Alice is exploring the wonderland, suddenly she fell into a hole, when she woke up, she found there are b - a + 1 treasures labled a from b in front of her. Alice was very excited but

zoj Treasure Hunt IV

Treasure Hunt IV Time Limit: 2 Seconds      Memory Limit: 65536 KB Alice is exploring the wonderland, suddenly she fell into a hole, when she woke up, she found there are b - a + 1 treasures labled a from b in front of her.Alice was very excited but

zoj 3629 Treasure Hunt IV 打表找规律

H - Treasure Hunt IV Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description Alice is exploring the wonderland, suddenly she fell into a hole, when she woke up, she found there are b - a + 1 treasures labled a from b in

zoj 3629 Treasure Hunt IV(找规律)

Alice is exploring the wonderland, suddenly she fell into a hole, when she woke up, she found there are b - a + 1 treasures labled a fromb in front of her.Alice was very excited but unfortunately not all of the treasures are real, some are fake.Now w

【树形dp】Treasure Hunt I

[ZOJ3626]Treasure Hunt I Time Limit: 2 Seconds      Memory Limit: 65536 KB Akiba is a dangerous country since a bloodsucker living there. Sometimes the bloodsucker will appear and kill everyone who isn't at his hometown. One day, a brave person named

POJ1066 Treasure Hunt(线段相交)

题目链接: http://poj.org/problem?id=1066 题目描述: Treasure Hunt Description Archeologists from the Antiquities and Curios Museum (ACM) have flown to Egypt to examine the great pyramid of Key-Ops. Using state-of-the-art technology they are able to determine

ZOJ 3626 Treasure Hunt I(树形dp)

Treasure Hunt I Time Limit: 2 Seconds      Memory Limit: 65536 KB Akiba is a dangerous country since a bloodsucker living there. Sometimes the bloodsucker will appear and kill everyone who isn't at his hometown. One day, a brave person named CC finds

POJ 1066 Treasure Hunt(计算几何)

Treasure Hunt Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5857   Accepted: 2439 Description Archeologists from the Antiquities and Curios Museum (ACM) have flown to Egypt to examine the great pyramid of Key-Ops. Using state-of-the-ar

zoj 3626 Treasure Hunt I (树形dp)

题目大意: 给出一棵树,求出从起点开始走m长度最后回到起点,所能得到的宝藏的最大价值. 思路分析: 通过一次dfs可以得到的是子树到根节点的所有距离的最大值. 现在的问题就是他走完一颗子树可以去另外一颗子树. 所以在回溯到根的时候要统计其他子树上互补距离的最大值. dp[i] [j] 表示i为根节点,在i的子树中走j步然后回到i所能拿到的最大价值. 转移方程就是 dp[x][i+2*len]=max(dp[x][i+2*len],dp[v][j]+dp[x][i-j]); v为x的子树的根,le