zoukankan      html  css  js  c++  java
  • HDU 4427 Math Magic【dp+优化+滚动数组】【好题】

    Math Magic

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2084    Accepted Submission(s): 692


    Problem Description
    Yesterday, my teacher taught us about math: +, -, *, /, GCD, LCM... As you know, LCM (Least common multiple) of two positive numbers can be solved easily because of a * b = GCD (a, b) * LCM (a, b).
    In class, I raised a new idea: “how to calculate the LCM of K numbers”. It's also an easy problem indeed, which only cost me 1 minute to solve it. I raised my hand and told teacher about my outstanding algorithm. Teacher just smiled and smiled...
    After class, my teacher gave me a new problem and he wanted me solve it in 1 minute, too.
    If we know three parameters N, M, K, and two equations:
    1. SUM (A1, A2, ..., Ai, Ai+1,..., AK) = N
    2. LCM (A1, A2, ..., Ai, Ai+1,..., AK) = M
    Can you calculate how many kinds of solutions are there for Ai (Ai are all positive numbers).
    I began to roll cold sweat but teacher just smiled and smiled. 
    Can you solve this problem in 1 minute?
     

    Input
    There are multiple test cases.
    Each test case contains three integers N, M, K. (1 <= N, M <= 1,000, 1 <= K <= 100)
     

    Output
    For each test case, output an integer indicating the number of solution modulo 1,000,000,007(109 + 7).
    You can get more details in the sample and hint below.
     

    Sample Input
    4 2 2 3 2 2
     

    Sample Output
    1 2
    Hint
    The first test case: the only solution is (2, 2). The second test case: the solution are (1, 2) and (2, 1).
     

    Source


    dp[i][j][k]。表示长度为i。和为j。最小公倍数为k的方法数。设a为解的第i+1个数。

    那么状态转移就为

    dp[i+1][j+a][lcm(a,k)]+=dp[i][j][k]。lcm为最大公倍数。

    因为开不了那么大的数组,因此要用滚动数组

    为了节约时间先预处理出1000以内任意两数的最小公倍数

    同时在循环的时候要注意剪枝(if (dp[(j-1)&1][i][last] == 0) continue;//如果上一状态为0则不用计算上一步)


    #include<iostream>	
    #include<algorithm>
    #include<cmath>
    #include<cstdio>
    #include<cstdlib>
    #include<queue>
    #include<map>
    #include<set>
    #include<stack>
    #include<bitset>
    #include<numeric>
    #include<vector>
    #include<string>
    #include<iterator>
    #include<cstring>
    #include<functional>
    #define INF 0x3f3f3f3f
    #define ms(a,b) memset(a,b,sizeof(a))
    using namespace std;
    
    const int maxn = 10010;
    const int mod = 1e9 + 7;
    const double pi = acos(-1.0);
    
    typedef pair<int, int> P;
    typedef long long ll;
    typedef unsigned long long ull;
    
    int gcd(int a, int b)
    {
    	return b == 0 ? a : gcd(b, a % b);
    }
    
    int lcm(int a, int b)
    {
    	return a / gcd(a, b) * b;
    }
    
    int N, M, n;
    int dp[2][1000 + 5][1000 + 5];
    int LCM[1000 + 5][1000 + 5];
    int fac[1000 + 5];
    
    int main()
    {
    	for (int i = 1; i <= 1000; i++)
    	{
    		for (int j = i; j <= 1000; j++)
    		{
    			LCM[i][j] = LCM[j][i] = lcm(i, j);
    		}
    	}
     	while (scanf("%d %d %d", &N, &M, &n) != EOF)
    	{
    		int cnt = 0;
    		for (int i = 1; i <= M; i++)
    		{
    			if (M%i == 0) fac[cnt++] = i;
    		}
    
    		ms(dp, 0);
    
    		for (int i = 0; i < cnt; i++)
    		{
    			dp[0][fac[i]][fac[i]] = 1;
    		}
    
    		for (int j = 1; j < n; j++)  //当前选的数  从j推j+1 故只需要推到n-1
    		{
    			ms(dp[j & 1], 0);
    			for (int i = j; i <= N; i++)//枚举和 j最小为1所以和最小只可能为1
    			{
    				for (int k = 0; k < cnt; k++)//枚举上一状态公倍数
    				{
    					int last = fac[k];
    					if (dp[(j-1)&1][i][last] == 0)	continue;//如果上一状态为0则不用计算上一步
    					for (int p = 0; p < cnt; p++)//枚举公因子	
    					{
    						int cur = fac[p];
    						if (i+cur<=N)//保证i+cur在范围内
    							dp[j & 1][i + cur][LCM[last][cur]] = (dp[j & 1][i + cur][LCM[last][cur]] + dp[(j-1)&1][i][last]) % 1000000007;
    					}
    				}
    			}
    		}
    		printf("%d
    ", dp[(n-1)& 1][N][M]);
    	}
    }




    Fighting~
  • 相关阅读:
    TREEVIEW拖拽对应修改目录
    shell脚本总结
    Linux将程序添加到服务的方法(通用【但最好还是用systemd】)
    Git客户端命令总结
    如何设置vim中tab键缩进---配置初始化设置
    在终端上创建Java项目及编译和运行
    Linux下Git安装及配置
    如何在eclipse的配置文件里指定jdk路径
    Redis总结和提取常用的和重要的命令
    Redis为什么是单线程
  • 原文地址:https://www.cnblogs.com/Archger/p/12774759.html
Copyright © 2011-2022 走看看