zoukankan      html  css  js  c++  java
  • GCD

    题目描述

    输入

     The first line is an positive integer  T . (1<=T<= 10^3) indicates the number of test cases. In the next T lines, there are three positive integer n, m, p (1<= n,m,p<=10^9) at each line.

    输出

    样例输入

    1 
    1 2 3
    
    

    样例输出

     

    1

    先把所有的1+Sn算出来,你会发现1+Sn=A(n+2);

    再双重循环算出前面几个gcd(1+Sn,1+Sm)%p,结果是A【gsd(n+2,m+2)】%p;

    直接循环解决就行了

    #include<stdio.h>
    long long gcd(long long a,long long b)
    {
    	long long t;
    	while(b)
    	{
    		t=a%b;
    		a=b;
    		b=t;
    	}
    	return a;
    }
    int main()
    {
    	long long t,m,n,p,i,sum,sum1,sum2,ans;
    	while(scanf("%lld",&t)!=EOF)
    	{
    		while(t--)
    		{
    			scanf("%lld%lld%lld",&n,&m,&p);
    			if(n>m)
    				ans=gcd(n+2,m+2);
    			else 
    				ans=gcd(m+2,n+2);
    			if(ans==0||ans==1||ans==2)
    			sum=1;
    			sum1=1;
    			sum2=1;
    			for(i=2;i<ans;i++)
    			{
    				sum=sum1+sum2;
    				sum=sum%p;
    				sum1=sum2;
    				sum2=sum;
    			}	
    			printf("%lld
    ",sum);
    		}
    	}
    	return 0;
    }

     

  • 相关阅读:
    Python staticmethod() 函数
    Python open() 函数
    Python input() 函数
    Python divmod() 函数
    Python abs() 函数
    instanceof和类型转换
    多态
    方法重写
    this
    Super详解
  • 原文地址:https://www.cnblogs.com/zyq1758043090/p/10003071.html
Copyright © 2011-2022 走看看