GCD2 -- SPOJ Solution C++

  Problem Link : GCD2  


👉 Hint : Big numbers with modulus

 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;

#define ll long long int
#define m 7

bool check(string s,int n)
{
	int rem=0;
	string t="";
	string temp;
	int val;
	while(1)
	{
	    if(rem>0)
	        temp=to_string(rem);
	    else
	        temp="";
	    int l=temp.length();
	    int q=m-l;
	    if(s.length()<=q)
	    {
	        t=temp+s;
	        val=stoi(t);
	        rem=val%n;
	        break;
	    }
		t=temp+s.substr(0,q);
		s=s.substr(q);

		val=stoi(t);
		rem=val%n;
	}
	if(rem==0)
		return 1;

	return 0;
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		string s;
		cin>>n>>s;
		if(n==0)
		{
		    cout<<s<<"\n";
		    continue;
		}
		for(int i=n;i>=1;i--)
		{
			if(n%i!=0)
				continue;
			if(check(s,i))
			{
				cout<<i<<"\n";
				break;
			}
		}
	}
}

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!