1005--Number Sequence

Insert picture description here

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int a,b;
long long n;
int f(int a,int b,long long n){
    
    
	if(n==1)
		return 1;
	else if(n==2)
		return 1;
 	else
		return (a*f(a,b,n-1)+b*f(a,b,n-2))%7;
}
int main()
{
    
    
	while(cin>>a>>b>>n){
    
    
		if(a==0 && b==0 && n==0)
			break;
		cout<<f(a,b,n)<<endl;
	}
	return 0;
}

Soon write the code above is a simple recursive function, turned out to be submitted confident Memory Limit Exceeded, in the final analysis is the number of iterations much exceeded the memory limit, the code is not good enough
rolled on CSDN, found it to be a bit mean The question is mainly because it is a bit controversial. Some people have made it but can’t explain why. I also use my superficial knowledge to explore: (see where the law is)

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int f(int a,int b,int n)
{
    
    
	if(n==1 || n==2)
		return 1;
    else
		return (a*f(a,b,n-1)+b*f(a,b,n-2))%7;
}
int main()
{
    
    
	int a,b,n;
	while(scanf("%d%d%d",&a,&b,&n) !=EOF){
    
    
		if(a==0 && b==0 && n==0)
			break;
		for(int i=1;i<=n;i++)
			cout<<f(a,b,i);
	}
	return 0;
}

The computer is slow to calculate during the test:

Insert picture description here

I closed it when there were less than fifty people, and it came out a bit slow, but we were really surprised to find that there was a pattern (1123160...), 16 cycles in a cycle. As for why, I should give a mathematician an explanation and try a few more sets:
Insert picture description here
14 A loop
Insert picture description here
This is a bit strange, right? . . .
Here, we first try to give the code of'AC' based on the sharing of netizens:

#include<iostream>
#include<cstdio>
using namespace std;
int a,b,n,c[100];
int main()
{
    
    
	while(cin>>a>>b>>n){
    
    
		if(a==0 && b==0 && n==0)
			break;
		c[1]=1;
		c[2]=1;
		for(int i=3;i<=48;i++){
    
    
			c[i]=(a*c[i-1]+b*c[i-2])%7;
		}
		cout<<c[n%48]<<endl;
	}
	return 0;
}

It’s over, but it’s not over yet: “
Let’s try the two test examples with the previous code first: (a=1, b=1 in the front, the following is a=1, b=2)
Insert picture description here

It is a period and the period is 6,
so we come to a conclusion: the period is there, but it cannot be measured by 48! In my example above, one cycle is 14, and one starts from the second position. Obviously, it cannot be solved by the so-called AC code. It can only be said that this is a joke. The test case cycle of Hangdian happens to be a factor of 48, so Luckily, if you want to get the correct answer, you have to find another way:

Matrix fast power

#include<iostream>
using namespace std;
struct M{
    
    
	int m[2][2];
};
int A,B;
M mul(M a,M b){
    
    
	M ans;
	for(int i=0;i<2;i++){
    
    
		for(int j=0;j<2;j++){
    
    
			ans.m[i][j] = 0;
			for(int k=0;k<2;k++)
				ans.m[i][j] = (ans.m[i][j]+a.m[i][k]*b.m[k][j]%7)%7;
		}
	}
	return ans;
}
M quick(long long b){
    
    
	M ans,c;
	ans.m[0][0] = ans.m[1][1] = 1;
	ans.m[0][1] = ans.m[1][0] = 0;
	c.m[0][0] = A,c.m[0][1] = B,c.m[1][0] = 1,c.m[1][1] = 0;
	while(b){
    
    
		if(b&1) ans = mul(ans,c);
		b >>= 1;
		c = mul(c,c);
	}
	return ans;
}
int main(){
    
    
	long long n;
	while(cin>>A>>B>>n){
    
    
		if(A==0&&B==0&&n==0) break;
		if(n<=2) cout<<"1"<<endl;
		else{
    
    
			M a = quick(n-2);
			cout<<(a.m[0][0]+a.m[0][1])%7<<endl;
		}
	}
}

Guess you like

Origin blog.csdn.net/interestingddd/article/details/113870720