GCD

問題 : GCD

時間限制: 1 Sec   內存限制: 1280 MB

題目描述

輸入

 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.css

輸出

樣例輸入

1 
1 2 3

樣例輸出

1

提示

gcd(1 + Sn, 1 + Sm) = gcd(fib(n + 2), fib(m + 2)) = fib(gcd(n + 2, m + 2)).markdown

#include <stdio.h>
int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;
}
int main()
{
    int t, n, m, p, s, a, b, c;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d%d%d", &n, &m, &p);
        s = gcd(n + 2, m + 2);
        a = 1;b = 1;c = 1;
        for (int i = 3; i <= s; i++)
        {
            c = (a + b) % p;
            a = b;
            b = c;
        }
        printf("%d\n", c);
    }
    return 0;
}
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息