zoj3662Math Magic
Math Magic
Time Limit: 3 Seconds Memory Limit: 32768 KB
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(1e9 + 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).
这题时间卡的真紧啊!
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
using namespace std;
#define MAXN 1005
#define mod 1000000007
int lca[MAXN][MAXN],dp[2][MAXN][MAXN],vec[MAXN];
int gcd(int a,int b)
{
if(a==0)return b;
return gcd(b%a,a);
}
int main()
{
int n,m,k,i,j,now,no,k1,j1,ans,ii;
for(i=1;i<=1000;i++)
for(j=i;j<=1000;j++)
lca[j][i]=lca[i][j]=i/gcd(i,j)*j;
while(scanf("%d%d%d",&n,&m,&no)!=EOF)
{
now=0;
//memset(dp,0,sizeof(dp));
ans=0;
vec[ans++]=1;
for(i=2;i<=m;i++)
{
if(m%i==0)
vec[ans++]=i;
}
for(ii=0;ii<=n;ii++)
for(j=0;j<ans;j++)
dp[now][ii][vec[j]]=0;
dp[now][0][1]=1;
for(i=0;i<=no-1;i++)
{
now=now^1;
for(ii=0;ii<=n;ii++)
for(j=0;j<ans;j++)
dp[now][ii][vec[j]]=0;
for(j=i;j<=n;j++)
for(int j2=0;j2<ans;j2++)
{
k=vec[j2];
if(dp[now^1][j][k]==0)
continue;
for(int jj1=0;jj1<ans;jj1++)
{ j1=vec[jj1];
if(j1+j>n)
break;
k1=lca[k][j1];
if(k1>m||m%k1!=0)
continue;
dp[now][j1+j][k1]+=dp[now^1][j][k]; dp[now][j1+j][k1]%=mod;
}
} }
printf("%d\n",dp[now][n][m]%mod);
}
return 0;
}
zoj3662Math Magic的更多相关文章
- Codeforces CF#628 Education 8 D. Magic Numbers
D. Magic Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- [8.3] Magic Index
A magic index in an array A[0...n-1] is defined to be an index such that A[i] = i. Given a sorted ar ...
- Python魔术方法-Magic Method
介绍 在Python中,所有以"__"双下划线包起来的方法,都统称为"Magic Method",例如类的初始化方法 __init__ ,Python中所有的魔 ...
- 【Codeforces717F】Heroes of Making Magic III 线段树 + 找规律
F. Heroes of Making Magic III time limit per test:3 seconds memory limit per test:256 megabytes inpu ...
- 2016中国大学生程序设计竞赛 - 网络选拔赛 C. Magic boy Bi Luo with his excited tree
Magic boy Bi Luo with his excited tree Problem Description Bi Luo is a magic boy, he also has a migi ...
- 一个快速double转int的方法(利用magic number)
代码: int i = *reinterpret_cast<int*>(&(d += 6755399441055744.0)); 知识点: 1.reinterpret_cast&l ...
- MAGIC XPA最新版本Magic xpa 2.4c Release Notes
New Features, Feature Enhancements and Behavior ChangesSubforms – Behavior Change for Unsupported Ta ...
- Magic xpa 2.5发布 Magic xpa 2.5 Release Notes
Magic xpa 2.5發佈 Magic xpa 2.5 Release Notes Magic xpa 2.5 Release NotesNew Features, Feature Enhance ...
- How Spring Boot Autoconfiguration Magic Works--转
原文地址:https://dzone.com/articles/how-springboot-autoconfiguration-magic-works In my previous post &qu ...
随机推荐
- (转)Ubuntu 16.04 安裝Docker(PS:本文适用amd64位的ubuntu系统)
1.前置安裝,確保你的系統是64位 $ sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ software-p ...
- 最大流KK算法
最大流KK算法 #include<iostream> #include<stdio.h> #include<string.h> #include<math.h ...
- bzoj5299: [Cqoi2018]解锁屏幕
题目链接 bzoj 5299: [Cqoi2018]解锁屏幕 题解 很水的装压dp,相信没人需要看题解.... dp[i][j]表示状态为i最后一个到的点为j,然后转移就很好写了 不过 我读入优化没读 ...
- []APC001
题目质量都好高啊... A:求一个是$X$的倍数但不是$Y$的倍数的数,无解输出$-1$ 无解就是$Y|X$,否则输出$X$即可 B:给定$a_{1\cdots n},b_{1\cdots n}$,求 ...
- bzoj1393 旅游航道
Description SGOI旅游局在SG-III星团开设了旅游业务,每天有数以万计的地球人来这里观光,包括联合国秘书长,各国总统和SGOI总局局长等.旅游线路四通八达,每天都有总躲得载客太空飞船在 ...
- 使用CefSharp在.Net程序中嵌入Chrome浏览器(二)——参数设置
在实现了.Net程序中嵌入Chrome浏览器后,下一步的个性化操作就是加入一些设置了,在前面的文章中,我们可以看到在使用Chrome控件前,有如下一个操作: var setting = new Cef ...
- eclipse.ini 文件使用说明
http://wiki.eclipse.org/Eclipse.ini Overview Eclipse startup is controlled by the options in $ECLIPS ...
- JavaScript在IE6下超级链接window.location.href不跳转的bug 及 解决方案
今天遇到个很诡异的问题,就是<a href="javascript:void(0);" onclick="window.location.href=url" ...
- redis.conf配置解释
daemonize:如果需要在后台运行,把该项改为yespidfile:配置多个pid的地址,默认在/var/run/redis.pidbind:绑定ip,设置后只接受来自该ip的请求port:监听端 ...
- .NET:异常处理的两条“黄金定律”,求批!
背景 架构之处必须考虑:如何处理异常?如何定义自己的异常体系?本文为了强化这个概念而写. 异常处理的两条“黄金定律” 自己抄袭的两条规律: 异常不能穿过“边界类”. 异常不能在没有恢复的情况下“吞掉” ...