![]() |
| | #1 |
| Registered User Join Date: Jan 2009
Posts: 65
| Discrete Math ![]() Given: Code: N , M , K , gcd(a,b)=1 N,M can be written as N = a + b M = a^2 + b^2 - (2^K - 2) * a * b Note: a,b are not given N ,M ~ 10^400 Now for this i write a brute force code and i always get gcd as a power of two i.e gcd(N,M)=2^z Hence for finding gcd i use a loop each time multiplying the gcd by 2 and checking if( N%gcd==0 && M%gcd===0 ) But this still ain't fast enough ! Is there any way to compute the gcd faster ?? Thanks |
| jack_carver is offline | |
| | #3 |
| Registered User Join Date: Jan 2009
Posts: 65
| Re I dont know what u meant but this is my current code Code: #include <cstdio>
#include <cstring>
using namespace std;
bool mod(const char *str,int num) //check if str%num==0
{
int i,len=strlen(str),res=0;
for(i=0;i<len;i++)
{
res=10*res+str[i]-'0';
res%=num;
}
return res==0;
}
int main()
{
int cases,K,gcd,temp;
char N[1005],M[1005];
scanf("%d",&cases);
while(cases--)
{
scanf("%s %s %d",N,M,&K);
gcd=1;
temp=1;
while(1)
{
if(mod(N,temp) && mod(M,temp))gcd=temp;
else break;
temp*=2;
}
printf("%d\n",gcd);
}
return 0;
}
|
| jack_carver is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Math | knightjp | General Discussions | 16 | 04-01-2009 05:36 PM |
| Basic Math Problem. Undefined Math Functions | gsoft | C Programming | 1 | 12-28-2004 03:14 AM |
| Discrete Math question | theoddmonkey | Tech Board | 2 | 07-01-2004 08:41 AM |
| toughest math course | axon | A Brief History of Cprogramming.com | 12 | 10-28-2003 10:06 PM |
| Discrete Math | rickc77 | A Brief History of Cprogramming.com | 4 | 02-19-2002 11:43 AM |