标签:规律
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 unfortunately not all of the treasures are real, some are fake.
Now we know a treasure labled n is real if and only if [n/1] + [n/2] + ... + [n/k] + ... is even.
Now given 2 integers a and b, your job is to calculate how many real treasures are there.
The input contains multiple cases, each case contains two integers a and b (0 <= a <= b <= 263-1) seperated by a single space. Proceed to the end of file.
Output the total number of real treasure.
0 2 0 10
1 6
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<algorithm> using namespace std; int main() { unsigned long long a,b; unsigned long long p1,p2; //printf("%lld\n",(1<<63)-1); unsigned long long ans,t1,t2; while(cin>>a>>b) { a++,b++; p1=sqrt(a+0.5); p2=sqrt(b+0.5); if(p1==p2) //如果他们落到了同一个地方 { if(p1&1) { if(p1*p1==a) printf("1\n"); else printf("0\n"); } else { if(p1*p1==a) cout<<b-a<<endl; else cout<<b-a+1<<endl; } continue; } ans=0; if(p1&1) //ok { if(p1*p1==a) ans+=1; t1=p1+2; } else { if(p1*p1==a) t1=p1+1; else { ans+=(p1+1)*(p1+1)-a+1; //就是这个地方爆的long long啊,巨坑。。 t1=p1+3; } } if(p2&1) { t2=p2; } else { ans+=b-p2*p2; t2=p2-1; } t1=(t1+1)/2,t2=(t2+1)/2,t1--; //下面是用公式计算中间的0,个数为1,5,9,13, //通项为4n-3,求和公式为(2n-1)*n if(t2>=t1) ans=ans+(2*t2-1)*t2-(2*t1-1)*t1; cout<<ans<<endl; } return 0; } /* 0 11538571374624767 5769285672726615 123 321 87 */
ZOJ3629 Treasure Hunt IV(找规律,推公式),布布扣,bubuko.com
ZOJ3629 Treasure Hunt IV(找规律,推公式)
标签:规律
原文地址:http://blog.csdn.net/coraline_m/article/details/26060077