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的更多相关文章

  1. 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 ...

  2. [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 ...

  3. Python魔术方法-Magic Method

    介绍 在Python中,所有以"__"双下划线包起来的方法,都统称为"Magic Method",例如类的初始化方法 __init__ ,Python中所有的魔 ...

  4. 【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 ...

  5. 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 ...

  6. 一个快速double转int的方法(利用magic number)

    代码: int i = *reinterpret_cast<int*>(&(d += 6755399441055744.0)); 知识点: 1.reinterpret_cast&l ...

  7. MAGIC XPA最新版本Magic xpa 2.4c Release Notes

    New Features, Feature Enhancements and Behavior ChangesSubforms – Behavior Change for Unsupported Ta ...

  8. 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 ...

  9. How Spring Boot Autoconfiguration Magic Works--转

    原文地址:https://dzone.com/articles/how-springboot-autoconfiguration-magic-works In my previous post &qu ...

随机推荐

  1. Appium robotframework-appium (ios 客户端测试)环境搭建

    一. 简介 1.1摘要 本人测试新人,最近在搞ios客户端的自动化,准备采用robotframework-appium来实现自动化测试,一边学习一边总结,此安装说明文档是基于mac系统10.11版本, ...

  2. [HNOI2008]玩具装箱TOY --- DP + 斜率优化 / 决策单调性

    [HNOI2008]玩具装箱TOY 题目描述: P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京. 他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器 ...

  3. 【20181027T1】洛阳怀【推结论+线性筛+分解质因数+GCD性质】

    原题:CF402D [错解] 唔,先打个表看看 咦,没有坏质数好像就是质因数个数啊 那有坏质数呢? 好像变负数了 推出错误结论:f(x)=x的质因数个数,如果有个坏质数,就乘上-1 然后乱搞,起码花了 ...

  4. 2017-2018-1 JAVA实验站 冲刺 day02

    2017-2018-1 JAVA实验站 冲刺 day02 各个成员今日完成的任务 小组成员 今日工作 完成进度 张韵琪 写博客.进行工作总结 100% 齐力锋 找背景音乐.开始界面图片.按钮图片.按钮 ...

  5. div块元素垂直水平居中方法总结

    1.已知块级元素的宽和高,使用绝对定位+外边距设定水平垂直居中. 父元素position:relative,子元素position:absolute;top:50%;left:50%;margin-t ...

  6. 如何使用Eclipse插件—Easy Explorer

    Easy Explorer是一个Eclipse插件,主要用于快速浏览项目文件的目录,实用性比较强. 使用方法如下: 1.下载Easy Explorer,从此处下载EasyExplorer:http:/ ...

  7. HDU 5646 DZY Loves Partition 数学 二分

    DZY Loves Partition 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5646 Description DZY loves parti ...

  8. 使用sklearn进行交叉验证

    模型评估方法 假如我们有一个带标签的数据集D,我们如何选择最优的模型? 衡量模型好坏的标准是看这个模型在新的数据集上面表现的如何,也就是看它的泛化误差.因为实际的数据没有标签,所以泛化误差是不可能直接 ...

  9. Shell基础学习(四) echo命令

    1.显示普通的字符串 echo "boring" 2.显示转义字符 echo "\"It is a test!\""; 3.read 命令从 ...

  10. JSoup 用法详解

    清单 1 // 直接从字符串中输入 HTML 文档 String html = "<html><head><title> 开源中国社区 </titl ...