ZZULI - 建国与两个数组(数学)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lzyws739307453/article/details/86100998

题目链接:http://acm.zzuli.edu.cn/problem.php?id=2502
时间限制: 1 Sec  内存限制: 128 MB

题目描述

    建勋这几天在学习关于数组的知识,他遇到了一个难题,用他学过的数组知识好像解决不了。建勋只好去求助聪明的建国,题目是这样的:有两个数组,第一个包含了1到n共n个数字,第二个包含了1到m共m个数字。建勋想要从两个数组中各挑选出一个整数x,y,使得x,y的和为k的倍数。
建国想利用这个机会考考你们,请问有多少种组合的方式?

输入

第一行输入一个整数T,表示样例数量。(1 <= T <= 1000)
接下来T行,每行输入三个整数n,m,k。(1 <= n, m, k <= 1000)

输出

对于每个样例,输出满足的对数。

样例输入

2
1 1 1
6 7 7

样例输出

1
6

提示

第一个样例只有(1,1)1种。
第二个样例有(1,6),(2,5),(3,4),(4,3),(5,2),(6,1)共6种。

解题思路

这一题是由牛客网上的一道题改编的,思路和那一题是一样的(https://blog.csdn.net/lzyws739307453/article/details/84677386)。

#include <stdio.h>
int main() {
    int t, n, m, k, a, b, ans;
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d%d", &m, &n, &k);
        a = m / k, b = n / k;
        m = m % k, n = n % k;
        ans = a * b * k + a * n + b * m;
        for (int i = 1; i <= n; i++)
            if ((m + i) % k == 0)
                ans += n - i + 1;
        printf("%d\n", ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lzyws739307453/article/details/86100998