zoj链接
hdu链接
Math Magic
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1653 Accepted Submission(s): 550
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
2012 Asia ChangChun Regional Contest
Recommend
zhuyuanchen520
题目大意:
求方程
sum(a1,a2,…ak)=n
lcm(a1,a2,…ak)=m
的解的组数。
分析:很容易能定义出状态f(i,j,k)表示选i个数,和为j,最小公倍数为k时的解的组数。观察一下发现如果通过填表来转移不易实现,考虑刷表转移。
设当前增加的数为v,那么转移时f(i+1,j+v,lcm(k,v))+=f(i,j,k)。
显然直接硬着转移每个状态一定TLE,我们来考虑一些优化。
首先,要求最后的lcm为m的话,那么中途枚举的数v一定是m的因数。另写一个程序我们可以发现1000以内因数个数最多也只有32个,则枚举v的时间复杂度降为常数级。另外,中途在转移时也只需要用到最小公倍数为m的因数的状态,因为只有这些状态最后才可能变为m,那么第三维状态也降为了常数(最多32)。那么对于一个确定的m,我们预处理出他的所有因数,第三维状态k的意义表示对应m因数的编号。总时间复杂度为O(k*N*A(m)^2)(A(m)为m因数的个数)。
空间复杂度为O(k*N*A(m))(当然,第一维可以滚动,但是空间足够并且编写麻烦而且中途涉及清空容易出错因此不再精益求精)。
详细内容见代码。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define maxn (1000)
#define clr(a,b) memset(a,b,sizeof(a))
using namespace std;
const int p=1e9+7;
int f[110][maxn+10][40];
int lcm[40][40],fac[40];
int n,m,s;
int gcd(int a,int b){
return !b?a:gcd(b,a%b);
}
int main(){
while(scanf("%d%d%d",&n,&m,&s)==3){
int cnt=0;
for(int i=1;i<=m;i++)
if(m%i==0)fac[++cnt]=i;//找出m的因数
clr(lcm,0);
for(int i=1;i<=cnt;i++)
for(int j=i;j<=cnt;j++){
int l=fac[i]*fac[j]/gcd(fac[i],fac[j]);
for(int k=j;k<=cnt;k++){
if(fac[k]==l){
lcm[i][j]=lcm[j][i]=k; /*预处理出lcm,
注意保存的是对应的编号,这是常数级时间内完成的*/
break;
}
}
}
clr(f,0);
for(int c=1;c<=cnt;c++)f[1][fac[c]][c]=1;
//把初始状态认为是放了一个数的情况
for(int i=1;i<s;i++){//当前数的个数
for(int j=1;j<=n;j++){//数的总和
for(int c1=1;c1<=cnt;c1++){//lcm
if(!f[i][j][c1])continue;
//如果为0没必要刷表了,常数优化
for(int c2=1;c2<=cnt;c2++){
//枚举当前加的数
int v=fac[c2];
if(j+v>n) continue;
f[i+1][j+v][lcm[c1][c2]]+=f[i][j][c1];
f[i+1][j+v][lcm[c1][c2]]%=p;
}
}
}
}
printf("%d\n",f[s][n][cnt]);
}
return 0;
}