• codeforces #305 A Mike and Frog


    挺简单的题目,但是有一堆恶心的边界

    在刨去恶心的边界之后:

    假定我们知道两边的循环节为b1,b2

    其中h第一次到达目标的时间为a1,a2

    又知道对于答案t

    t=a1+b1*t1=a2+b2*t2

    不妨枚举t1,判断是否存在可行解即可

    又因为LCM(b1,b2)就开始循环了

    且b1*b2<=b1*mod

    所以我们枚举t1的范围在[0,mod]即可

    如果在这个范围内无解,则一定无解

    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    typedef long long LL;
    bool vis[1000010];
    int mod,h,a,x,y;
    int Go(int h,int x,int y){
    	memset(vis,0,sizeof(vis));
    	int cnt=0;
    	while(1){
    		if(vis[h])return -1;
    		vis[h]=1;
    		h=(1LL*h*x+y)%mod;
    		++cnt;
    		if(h==a)return cnt;
    	}
    }
    
    int main(){
    	scanf("%d",&mod);
    	scanf("%d%d%d%d",&h,&a,&x,&y);
    	int a1=Go(h,x,y),b1=Go(a,x,y);
    	scanf("%d%d%d%d",&h,&a,&x,&y);
    	int a2=Go(h,x,y),b2=Go(a,x,y);
    	if(a1==-1||a2==-1){printf("-1
    ");return 0;}
    	if(b1==-1&&b2==-1){
    		if(a1==a2)printf("%d
    ",a1);
    		else printf("-1
    ");
    		return 0;
    	}
    	if(b1!=-1&&b2!=-1){
    		for(int i=0;i<=mod;++i){
    			if(a1+1LL*b1*i>=a2&&(a1+1LL*b1*i-a2)%b2==0){
    				cout<<a1+1LL*b1*i<<endl;
    				return 0;
    			}
    		}printf("-1
    ");
    		return 0;
    	}else{
    		if(b1==-1)swap(a1,a2),swap(b1,b2);
    		if(a2>=a1&&(a2-a1)%b1==0)printf("%d
    ",a2);
    		else printf("-1
    ");
    	}return 0;
    }
    

      

  • 相关阅读:
    绝对定位和相对定位的内幕
    水平居中和垂直居中
    玩转html5<canvas>画图
    基本排序算法
    很好用的canvas
    IE浏览器存在的setAttribute bug
    js 高程 函数节流 throttle() 分析与优化
    js apply()、call() 使用参考
    js 高程 22.1.4 函数绑定 bind() 封装分析
    事件处理程序中 this 的指向
  • 原文地址:https://www.cnblogs.com/joyouth/p/5352953.html
Copyright © 2020-2023  润新知