HDU —— 6225

Little Boxes

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1779    Accepted Submission(s): 664



Problem Description
Little boxes on the hillside.
Little boxes made of ticky-tacky.
Little boxes.
Little boxes.
Little boxes all the same.
There are a green boxes, and b pink boxes.
And c blue boxes and d yellow boxes.
And they are all made out of ticky-tacky.
And they all look just the same.
 

Input
The input has several test cases. The first line contains the integer t (1 ≤ t ≤ 10) which is the total number of test cases.
For each test case, a line contains four non-negative integers a, b, c and d where a, b, c, d ≤ 2^62, indicating the numbers of green boxes, pink boxes, blue boxes and yellow boxes.
 

Output
For each test case, output a line with the total number of boxes.
 

Sample Input
4 1 2 3 4 0 0 0 0 1 0 0 0 111 222 333 404
 

Sample Output
10 0 1 1070
 

这题把我搞死。。。满心欢喜的觉得超简单很快能过,

第一遍long long 直接加 wa,,,,,

第二遍unsighned long long     wa

第三遍改用大数,,,wa

第四遍大数修改,,,wa(再改一点就过)

第五遍想了一个超级超级巧妙的方法,贴错了代码。。。错了代码。。。代码。。。(这种方法可以过的)

第六遍,,队友过(论有个好队友的重要性)

可长点心吧,这题耽误时间我的锅。。。

方法一:

思路:unsigned long long ,所以至少可以用两个long long 分别加两个数,话不多说贴代码

#include<bits/stdc++.h>
using namespace std;
const int MAX = 80;
int x[MAX];
int y[MAX];
int ans[MAX];
int main()
{
    int t;
    unsigned long long a, b, c, d, temp1, temp2;
    while(scanf("%d", &t) != EOF)
    {
        while(t--)
        {
            memset(ans, 0, sizeof(ans));
            memset(x, 0, sizeof(x));
            memset(y, 0, sizeof(y));
            scanf("%lld %lld %lld %lld", &a, &b, &c, &d);
            temp1 = a+b;
            temp2 = c+d;
            int cnt1 = 79, cnt2 = 79;
            while(temp1)
            {
                x[cnt1--] = temp1%10;
                temp1 /= 10;
            }
            while(temp2)
            {
                y[cnt2--] = temp2%10;
                temp2 /= 10;
            }
            int temp = 79;
            while(temp >= cnt1 && temp >= cnt2)
            {
                if(x[temp] + y[temp] >= 10 - ans[temp])
                {
                    ans[temp] = (ans[temp] + x[temp] + y[temp])%10;
                    ans[temp-1]++;
                }
                else
                    ans[temp] += (x[temp] + y[temp]);
                temp--;
            }
            while(temp >= cnt1)
            {
                if(x[temp] + ans[temp] > 9)
                {
                    ans[temp] = (x[temp] + ans[temp])%10;
                    ans[temp-1]++;
                }
                else
                    ans[temp] += x[temp];
                temp--;
            }
            while(temp >= cnt2)
            {
                if(y[temp] + ans[temp] > 9)
                {
                    ans[temp] = (y[temp] + ans[temp])%10;
                    ans[temp-1]++;
                }
                else
                    ans[temp] += y[temp];
                    temp--;
            }
            while(ans[temp] == 0 && temp != 79)
                temp++;
            for(int i = temp; i <= 79; i++)
            {
                cout<<ans[i];
            }
            cout<<endl;
        }
    }
    return 0;
}


方法二:

思路:四个数相加最大是2^64,unsighned long long 最大为2^64-1,所以当四个数全是2^62时

输出2^64,其他情况正常相加后输出

按例贴代码

#include<bits/stdc++.h>
using namespace std;
const int MAX = 80;
int x[MAX];
int y[MAX];
int ans[MAX];
int main()
{
	int t;
	unsigned long long a, b, c, d, temp1, temp2;
	scanf("%d", &t);
	while(t--)
	{
		scanf("%lld %lld %lld %lld", &a, &b, &c, &d);
		temp1 = a+b;
		temp2 = c+d; 
		if(a ==  4611686018427387904 && b == 4611686018427387904 && c == 4611686018427387904 && d == 4611686018427387904)
			cout<<"18446744073709551616"<<endl;
		else
			cout<<temp1 + temp2<<endl;
	}
	return 0;
}



### HDU OJ Problem 2566 Coin Counting Solution Using Simple Enumeration and Generating Function Algorithm #### 使用简单枚举求解硬币计数问题 对于简单的枚举方法,可以通过遍历所有可能的组合方式来计算给定面额下的不同硬币组合数量。这种方法虽然直观但效率较低,在处理较大数值时性能不佳。 ```java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int[] coins = {1, 2, 5}; // 定义可用的硬币种类 while (scanner.hasNext()) { int targetAmount = scanner.nextInt(); int countWays = findNumberOfCombinations(targetAmount, coins); System.out.println(countWays); } } private static int findNumberOfCombinations(int amount, int[] denominations) { if (amount == 0) return 1; if (amount < 0 || denominations.length == 0) return 0; // 不使用当前面值的情况 int excludeCurrentDenomination = findNumberOfCombinations(amount, subArray(denominations)); // 使用当前面值的情况 int includeCurrentDenomination = findNumberOfCombinations(amount - denominations[0], denominations); return excludeCurrentDenomination + includeCurrentDenomination; } private static int[] subArray(int[] array) { if (array.length <= 1) return new int[]{}; return java.util.Arrays.copyOfRange(array, 1, array.length); } } ``` 此代码实现了通过递归来穷尽每一种可能性并累加结果的方式找到满足条件的不同组合数目[^2]。 #### 利用母函数解决硬币计数问题 根据定义,可以将离散序列中的每一个元素映射到幂级数的一个项上,并利用这些多项式的乘积表示不同的组合情况。具体来说: 设 \( f(x)=\sum_{i=0}^{+\infty}{a_i*x^i}\),其中\( a_i \)代表当总金额为 i 时能够组成的方案总数,则有如下表达式: \[f_1(x)=(1+x+x^2+...)\] 这实际上是一个几何级数,其封闭形式可写作: \[f_1(x)=\frac{1}{(1-x)}\] 同理,对于其他类型的硬币也存在类似的生成函数。因此整个系统的生成函数就是各个单独部分之积: \[F(x)=f_1(x)*f_2(x)...*f_n(x)\] 最终目标是从 F(x) 中提取系数即得到所需的结果。下面给出基于上述理论的具体实现: ```cpp #include<iostream> using namespace std; const int MAXN = 1e4 + 5; int dp[MAXN]; void solve() { memset(dp, 0, sizeof(dp)); dp[0] = 1; // 初始化基础状态 int values[] = {1, 2, 5}, size = 3; for (int j = 0; j < size; ++j){ for (int k = values[j]; k <= 10000; ++k){ dp[k] += dp[k-values[j]]; } } } int main(){ solve(); int T; cin >> T; while(T--){ int n; cin>>n; cout<<dp[n]<<endl; } return 0; } ``` 这段 C++ 程序展示了如何应用动态规划技巧以及生成函数的概念高效地解决问题实例[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值