Saturday, April 16, 2016

UVA Problem 11417: GCD -Solution

Unknown
#include<cstdio>
using namespace std;

int ans[502]={0},sum={0};

int gcd(int a, int b)
{
    if (b==0) return a;
    return gcd(b,a%b);
}
int gcdsum(int n)
{
    int s=0;
    for (int i=1;i<n;i++) s+=gcd(n,i);
    return s;
}

int calc(int n)
{
    if (n==2) return 1;
    if (ans[n]!=0) return ans[n];
    ans[n]=calc(n-1)+gcdsum(n);
    return ans[n];
}

int main()
{
    int n;
    while (1)
    {
        scanf("%d",&n);
        if (n==0) break;
        printf("%d\n",calc(n));
    }
    return 0;
}

Unknown

Studying at Shahjalal University of Science and Technology, Sylhet.

0 comments:

Post a Comment

Coprights @ 2016,