HDU1792A New Change Problem(GCD规律推导)

A New Change Problem

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 533    Accepted Submission(s): 265


Problem Description
Now given two kinds of coins A and B,which satisfy that GCD(A,B)=1.Here you can assume that there are enough coins for both kinds.Please calculate the maximal value that you cannot pay and the total number that you cannot pay.
 
Input
The input will consist of a series of pairs of integers A and B, separated by a space, one pair of integers per line.
 
Output
For each pair of input integers A and B you should output the the maximal value that you cannot pay and the total number that you cannot pay, and with one line of output for each line in input.
 
Sample Input
2 3 3 4
 
Sample Output
1 1 5 3
 
Author
lcy
 
/*************************************************************************
    > File Name: HDU1792.cpp
    > Author: LyuCheng
    > Created Time: 2017年10月19日 星期四 02时56分57秒
 ************************************************************************/
/*
 * 题意:给你两个素数,问你用这两个素数最大不能组成的数是什么,和有多少不能组成的数。
 *      每个素数都可以用无数次
 *
 * 思路:A*x+B*y=t;
 *      最大的不能组成:
 *              那么t可以分成两部分,一部分是能让A整除的,另一部分是mod A的余数,当t>=A*B-B=B*(A-1)
 *          时,1~(B*(A-1)) mod A 可以形成0~A-1的任何一个数,所以t>=A*B-B时一定能被组成,现在的问题
 *          就是找小于A*B-B的第一个不能组成的,不能组成的条件就是小于A*B-B并且与他膜A同余的第一个数
 *          也就是A*B-B-A
 *
 *      有多少不能组成的数:
 *              从上边知道一个数不能组成的条件就是这个数是B的倍数,并且小于A*x+B*y且与他同余,那么有
 *          (A-1)*B: (A-1)*B-A , (A-1)*B-2*A ... (A-1)*B/A-1个,(之所以减一因为(A-1)*B是可以被组成的)
 *          (A-2)*B: (A-2)*B-A , (A-2)*B-2*A ...
 *          (A-3)*B: (A-3)*B-A , (A-3)*B-2*A ...
 *          ...
 *          B: B-A, ...
 *
 *          那么结果
 *              res= (A-1)*B/A-1+(A-1)*B/A-1+...+B/A-1
 *                 = (A-1)*(B-1)/2;
 * */
#include <bits/stdc++.h>

using namespace std;

int a,b;

int main(){
    while(scanf("%d%d",&a,&b)!=EOF){
            printf("%d %d
",a*b-a-b,(a-1)*(b-1)/2);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/7691439.html