HDOJ Just Random 4790【2013成都区域赛J题+等差数列优化】
来源:互联网 发布:金越软件招聘 编辑:程序博客网 时间:2024/05/22 14:31
Just Random
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2102 Accepted Submission(s): 592
Problem Description
Coach Pang and Uncle Yang both love numbers. Every morning they play a game with number together. In each game the following will be done:
1. Coach Pang randomly choose a integer x in [a, b] with equal probability.
2. Uncle Yang randomly choose a integer y in [c, d] with equal probability.
3. If (x + y) mod p = m, they will go out and have a nice day together.
4. Otherwise, they will do homework that day.
For given a, b, c, d, p and m, Coach Pang wants to know the probability that they will go out.
1. Coach Pang randomly choose a integer x in [a, b] with equal probability.
2. Uncle Yang randomly choose a integer y in [c, d] with equal probability.
3. If (x + y) mod p = m, they will go out and have a nice day together.
4. Otherwise, they will do homework that day.
For given a, b, c, d, p and m, Coach Pang wants to know the probability that they will go out.
Input
The first line of the input contains an integer T denoting the number of test cases.
For each test case, there is one line containing six integers a, b, c, d, p and m(0 <= a <= b <= 109, 0 <=c <= d <= 109, 0 <= m < p <= 109).
For each test case, there is one line containing six integers a, b, c, d, p and m(0 <= a <= b <= 109, 0 <=c <= d <= 109, 0 <= m < p <= 109).
Output
For each test case output a single line "Case #x: y". x is the case number and y is a fraction with numerator and denominator separated by a slash ('/') as the probability that they will go out. The fraction should be presented in the simplest form (with the smallest denominator), but always with a denominator (even if it is the unit).
Sample Input
40 5 0 5 3 00 999999 0 999999 1000000 00 3 0 3 8 73 3 4 4 7 0
Sample Output
Case #1: 1/3Case #2: 1/1000000Case #3: 0/1Case #4: 1/1
Source
2013 Asia Chengdu Regional Contest
Recommend
We have carefully selected several similar problems for you: 5338 5337 5336 5335 5334
终于看懂这题的代码了。。。
斌神代码:点击打开链接
利用等差数列优化。。。等差数列公式:
等差数列前n项和公式
注意计算的位置 计算末项时记得算前面的。
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;typedef long long LL;LL gcd(LL a,LL b){if(b==0)return a;return gcd(b,a%b);}int main(){int T;scanf("%d",&T);int xp=1;while(T--){LL a,b,c,d,p,m;scanf("%lld%lld%lld%lld%lld%lld",&a,&b,&c,&d,&p,&m);if((a+d)<(b+c)){swap(a,c);swap(b,d);}LL ans=0;LL t1=(a+c)%p; LL add=(m-t1+p)%p;LL cnt1=(a+c+add-m)/p;//计算a+c后面的第一个符合要求的数LL t2=(b+c-1)%p;LL s=(t2-m+p)%p;//因为要计算前一个数所以要减去s,为了符合等式减去的是p-(m-t2). LL cnt2=(b+c-1-s-m)/p;//计算b+c前面的第一个符合要求的数ans+=(cnt2-cnt1+1)*(1+add)+(cnt2-cnt1+1)*(cnt2-cnt1)/2*p;t1=(b+c)%p;add=(m-t1+p)%p;cnt1=(b+c+add-m)/p;t2=(a+d)%p;s=(t2-m+p)%p;cnt2=(a+d-s-m)/p;ans+=(cnt2-cnt1+1)*(b-a+1);t1=(a+d+1)%p;add=(m-t1+p)%p;cnt1=(a+d+1+add-m)/p;t2=(b+d)%p;s=(t2-m+p)%p;cnt2=(b+d-s-m)/p;ans+=(cnt2-cnt1+1)*(1+s)+(cnt2-cnt1+1)*(cnt2-cnt1)/2*p;long long num=(b-a+1)*(d-c+1);LL GCD=gcd(ans,num);ans/=GCD;num/=GCD;printf("Case #%d: %lld/%lld\n",xp++,ans,num);}return 0;}
0 0
- HDOJ Just Random 4790【2013成都区域赛J题+等差数列优化】
- HDU 4790 Just Random (2013ACM/ICPC成都赛区现场赛J题)
- HDU 4790 2013 ACM/ICPC 成都赛区现场赛 J. Just Random
- hdu 4790 Just Random(2013成都)
- HDOJ 4790 Just Random
- hdoj 4790 Just Random 【数学】
- hdoj-4790-Just Random【数论】
- HDOJ Hard Disk Drive 4788【2013成都区域赛H题-水】
- HDU 4473 Exam 有点技巧的暴力枚举 (2012成都区域赛J题)
- 2013 成都区域赛 总结
- 2013成都站J题||hdu4790 数论
- hdu 4790 Just Random
- hdu 4790 Just Random
- HDU 4790 Just Random
- HDU - 4790 Just Random
- HDU 4790 Just Random
- HDU 4790 Just Random
- hdu 4790 Just Random
- 很好的网站
- hdoj 2032 杨辉三角
- grep 命令
- 用qt的图形视图框架实现的圆,三角形,矩形运动及碰撞.
- 为什么计算机的学生要学习 Linux 开源技术
- HDOJ Just Random 4790【2013成都区域赛J题+等差数列优化】
- js 获取访问链接参数
- 如果让我重新设计一款Android App
- HDU 1203 I NEED A OFFER!
- KindEditor保留htmltag,以及关闭html过滤
- VC6.0编写IE可运行的ActiveX,增加安全检测项,不支持64位浏览器
- 3D打印机切片引擎Cura分析<二>记一个小软件
- HDOJ 1000 1001丶
- 权利指数(hdu 1557)