题目链接:Just Random
题面:
4 0 5 0 5 3 0 0 999999 0 999999 1000000 0 0 3 0 3 8 7 3 3 4 4 7 0
Case #1: 1/3 Case #2: 1/1000000 Case #3: 0/1 Case #4: 1/1
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <map> #include <cmath> #include <cstdlib> #include <set> #include <algorithm> #include <string> #include <iomanip> #define LL long long using namespace std; LL min(LL a,LL b) { if(a<=b)return a; else return b; } LL max(LL a,LL b) { if(a>=b)return a; else return b; } LL gcd(LL a,LL b) { if(a==0)return b; return gcd(b%a,a); } int main() { int t; cin>>t; LL a,b,c,d,m,p,ans,x,start,num,line,tmp,bound,lim,total,gcdd; bool flag; for(int cas=1;cas<=t;cas++) { flag=false; ans=0; cout<<"Case #"<<cas<<": "; cin>>a>>b>>c>>d>>p>>m; line=min(b-a,d-c)+(a+c); //cout<<"line: "<<line<<endl; tmp=min(b-a,d-c); bound=max((b-a),(d-c))+1; //cout<<"tmp: "<<tmp<<endl; x=a+c; if(p<10000) { for(int i=1;i<=tmp;i++) { if(x%p==m) { start=i; //cout<<"p: "<<p<<endl; //cout<<"x: "<<x<<endl; //cout<<"start: "<<start<<endl; flag=true; break; } x++; } if(flag) { num=(line-1-x)/p+1; //cout<<"num: "<<num<<endl; ans+=(start+(start+(num-1)*p))*num/2; //cout<<"* "<<ans<<endl; flag=false; } //cout<<"line: "<<line<<endl; for(int i=tmp+1,x=line;i<=bound;i++) { if(x%p==m) { start=i; flag=true; break; } x++; } if(flag) { num=(bound-start)/p+1; ans+=num*min(b-a+1,d-c+1); //cout<<"** "<<ans<<endl; flag=false; } lim=(b-a)+(d-c)+1; for(int i=tmp,x=bound+(a+c);i>=1;i--) { if(x%p==m) { //cout<<"x: "<<x<<endl; start=i; //cout<<"start: "<<start<<endl; flag=true; break; } x++; } if(flag) { num=(start)/p+1; //cout<<"num: "<<num<<endl; //cout<<"start: "<<start<<endl; ans+=(start+(start-(num-1)*p))*num/2; } } else { flag=false; start=m; while(1) { if(start>=x) { if(start<line) flag=true; break; } start+=p; } if(flag) { num=(line-1-start)/p+1; tmp=start-x+1; ans+=(tmp+tmp+(num-1)*p)*num/2; flag=false; } start=(line/p)*p+m; lim=line+abs((b-a)-(d-c)); while(1) { if(start>=line) { if(start<=lim) flag=true; break; } start+=p; } if(flag) { num=(lim-start)/p+1; tmp=min((b-a),(d-c))+1; ans+=num*tmp; flag=false; } start=((lim+1)/p)*p+m; bound=a+c+(b-a)+(d-c); while(1) { if(start>lim) { if(start<=bound) flag=true; break; } start+=p; } if(flag) { num=(bound-start)/p+1; tmp=min((b-a),(d-c))+1-(start-lim); ans+=(tmp+(tmp-(num-1)*p))*num/2; } } total=(b-a+1)*(d-c+1); gcdd=gcd(ans,total); cout<<ans/gcdd<<"/"<<total/gcdd<<endl; } return 0; }
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <map> #include <cmath> #include <cstdlib> #include <set> #include <algorithm> #include <string> #include <iomanip> #define LL long long using namespace std; LL min(LL a,LL b) { if(a<=b)return a; else return b; } LL max(LL a,LL b) { if(a>=b)return a; else return b; } LL gcd(LL a,LL b) { if(a==0)return b; return gcd(b%a,a); } int main() { int t; cin>>t; LL a,b,c,d,m,p,ans,x,start,num,line,tmp,bound,lim,total,gcdd; bool flag; for(int cas=1;cas<=t;cas++) { flag=false; ans=0; cout<<"Case #"<<cas<<": "; cin>>a>>b>>c>>d>>p>>m; line=min(b-a,d-c)+(a+c); //cout<<"line: "<<line<<endl; tmp=min(b-a,d-c); bound=max((b-a),(d-c))+1; //cout<<"tmp: "<<tmp<<endl; x=a+c; flag=false; start=(a+c)/p*p+m; while(1) { if(start>=x) { if(start<line) flag=true; break; } start+=p; } if(flag) { num=(line-1-start)/p+1; tmp=start-x+1; ans+=(tmp+tmp+(num-1)*p)*num/2; flag=false; } start=(line/p)*p+m; lim=line+abs((b-a)-(d-c)); while(1) { if(start>=line) { if(start<=lim) flag=true; break; } start+=p; } if(flag) { num=(lim-start)/p+1; tmp=min((b-a),(d-c))+1; ans+=num*tmp; flag=false; } start=((lim+1)/p)*p+m; bound=a+c+(b-a)+(d-c); while(1) { if(start>lim) { if(start<=bound) flag=true; break; } start+=p; } if(flag) { num=(bound-start)/p+1; tmp=min((b-a),(d-c))+1-(start-lim); ans+=(tmp+(tmp-(num-1)*p))*num/2; } total=(b-a+1)*(d-c+1); gcdd=gcd(ans,total); cout<<ans/gcdd<<"/"<<total/gcdd<<endl; } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/david_jett/article/details/46907521