HDU 2662 Coin && HDU 1792 A New Change Problem (互质数最大不能生成数)

证明见here,写的很不错

2662 Coin

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 903    Accepted Submission(s): 437

Problem Description

Moon has many coins, but only contains two value types which is 5 cents and 7 cents, Some day he find that he can get any value which greater than 23 cents using some of his coins. For instance, he can
get 24 cents using two 5 cents coins and two 7 cents coins, he can get 25 cents using five 5 cents coins, he can get 26 cents using one 5 cents coins and three 7 cents coins and so on.

Now, give you many coins which just contains two value types just like Moon, and the two value types identified by two different prime number i and j. Can you caculate the integer n that any value greater than n can be created by some of the given coins.

Input

The first line contains an integer T, indicates the number of test cases.

For each test case, there are two different prime i and j separated by a single space.(2<=i<=1000000, 2<=j<=1000000)

Output

For each test case, output one line contains the number n adapt the problem description.

Sample Input

1
5 7

Sample Output

23

Source

HDU男生专场公开赛——赶在女生之前先过节(From
WHU)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2662

题目大意:给两个互质的数,求用无限个它们不能组成的最大的数

题目分析:当定理记吧,ans = n * m - n - m

#include <cstdio>
#define ll long long

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        ll a, b;
        scanf("%I64d %I64d", &a, &b);
        printf("%lld\n", a * b - a - b);
    }
}

1792 A New Change Problem

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 936    Accepted Submission(s): 515

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1792

题目大意:给两个互质的数,求用无限个它们不能组成的最大的数和不能组成的数的个数

题目分析:当定理记吧,不能组成的最大数n * m - n - m,不能组成的个数(n - 1) * (m - 1) / 2

#include <cstdio>

int main()
{
    int a, b;
    while(scanf("%d %d", &a, &b) != EOF)
        printf("%d %d\n", a * b - a - b, (a - 1) * (b - 1) / 2);
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-15 20:43:50

HDU 2662 Coin && HDU 1792 A New Change Problem (互质数最大不能生成数)的相关文章

hdu 1792 A New Change Problem(互质数之间最大不能组合数和不能组合数的个数)

题意:求互质的m和n的最大不能组合数和不能组合数的个数 思路:m和n的最大不能组合数为m*n-m-n,不能组合数的个数为(m-1)*(n-1)/2 推导: 先讨论最大不能组合数 因为gcd(m,n)=1,所以 0,n,2*n,3*n,...(m-1)*n(共m个数字)分别除以m,余数肯定不同,且为{0,1,2,3...m-1}中的某数 若存在非负数p,q使得pm+qn=x,x为可组合值,两边对m取余,则(q*n)%m==x%m,p*m>=0,所以只要x>q*n,x都能被组合出来.当q<m

HDU 1792-A New Change Problem(互质数的最大不能表示数)

题目地址:HDU 1792 题意:两个互质的数A,B且A*x+B*y(x>=0,y>=0)求最大不能表示的数,和不能表示的数的个数. 思路:最大不能表示的数是A*B-A-B,个数是(A-1)*(B-1)/2.详细推导 #include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <

HDU 2069 Coin Change 母函数求解

HDU 2069 Coin Change 母函数求解 题目:http://acm.hdu.edu.cn/showproblem.php?pid=2069 这题比普通的母函数求解题目复杂一点,多了组成个数的限制, 要求组合个数不能超过 100 个硬币,所以将 c1, c2 定义为二维数组, c1[i][j] 表示 c1[结果][组成该结果的个数] ,然后多了一层遍历个数的循环. // 母函数求解 #include <bits/stdc++.h> using namespace std; cons

UVa 674 &amp; hdu 2069 Coin Change (母函数,dp)

链接:uva 674 题意:有5中货币,价值分别为 50-cent, 25-cent, 10-cent, 5-cent,1-cent,数量都为无限个, 给定一个数 n,求用上述货币组成价值为 n 的方法有多少? 分析:因为n<=7489,可以用 母函数 或 dp 打表 对于dp状态方程为: dp[j]+=dp[j-c[i]] 母函数: #include<stdio.h> int c1[7500],c2[7500],w[5]={1,5,10,25,50};; void mhs() { in

POJ 2135 Farm Tour &amp;&amp; HDU 2686 Matrix &amp;&amp; HDU 3376 Matrix Again 费用流求来回最短路

累了就要写题解,最近总是被虐到没脾气. 来回最短路问题貌似也可以用DP来搞,不过拿费用流还是很方便的. 可以转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1,然后连一条流量为1(花费根据题意来定) 的边来控制每个点只能通过一次. 额外添加source和sink来控制满流为2. 代码都雷同,以HDU3376为例. #include <algorithm> #include <iostream> #include <cstring> #in

HDU 1086:You can Solve a Geometry Problem too

You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6997    Accepted Submission(s): 3385 Problem Description Many geometry(几何)problems were designed in the ACM/

hdu 2686 Matrix &amp;&amp; hdu 3367 Matrix Again (最大费用最大流)

Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1394    Accepted Submission(s): 758 Problem Description Yifenfei very like play a number game in the n*n Matrix. A positive integer number

HDU 1506 &amp;&amp; HDU1505 &amp;&amp; HDU 2870 (DP).

~~~~ 这三道DP题是逐层递进的,大家可以从前往后做. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1506 http://acm.hdu.edu.cn/showproblem.php?pid=1505 http://acm.hdu.edu.cn/showproblem.php?pid=2870 ~~~~ 1506: 分别找每一块板子的可以的最左边界和最右边界,注意这时不能用两个for暴力(显然会TLE),所以要用DP的思想边搜边保存. 另外注

HDU 4793 Collision + HDU 4798 Skycity 简单几何

HDU 4793 链接:http://acm.hdu.edu.cn/showproblem.php?pid=4793 题意:给一个以(0,0)为圆心半径为R的圆形区域,中间放着一个(0,0)为圆心半径为Rm的圆盘,在坐标(x,y)处(严格在圆形区域外)放着一枚半径为r的硬币,运动方向和速度为(vx,vy),在运动中碰到圆盘时,会按碰撞问题反弹(圆盘是固定不动的),问硬币会在圆形区域里呆多长时间(硬币只要有一点点在圆形区域里就记为硬币在圆形区域内). 思路:首先先计算出硬币在移动过程中如果不与圆盘