GCD

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_41505957/article/details/79968081

题目描述

输入

 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\n",sum);
		}
	}
	return 0;
}

 

 

 

 

 

阅读更多

没有更多推荐了,返回首页