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 ...
随机推荐
- 【BZOJ 3812】 3812: 主旋律 (容斥原理**)
3812: 主旋律 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 235 Solved: 196 Description 响应主旋律的号召,大家决定 ...
- 【BZOJ 3144】 3144: [Hnoi2013]切糕 (最小割模型)
3144: [Hnoi2013]切糕 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1764 Solved: 965 Description Inp ...
- 【BZOJ 2119】 2119: 股市的预测 (后缀数组+分块+RMQ)
2119: 股市的预测 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 404 Solved: 188 Description 墨墨的妈妈热爱炒股,她 ...
- luoguP4457 [BJOI2018]治疗之雨 概率期望 + 高斯消元
应该是最后一道紫色的概率了....然而颜色啥也代表不了.... 首先看懂题意: 你现在有$p$点体力,你的体力上限为$n$ 在一轮中, 1.如果你的体力没有满,你有$\frac{1}{m + 1}$的 ...
- 【插头DP】BZOJ3125-city
开学忙成狗,刷题慢如蜗牛…… [题目大意] 给出一个m*n的矩阵里面有一些格子为障碍物,一些格子只能上下通行,一些格子只能左右通行,一些格子上下左右都能通行.问经过所有非障碍格子的哈密顿回路个数. [ ...
- 腾讯通消息webSDK踩坑
1.腾讯通提供一个通过http协议的接口,可用于发送消息,公告等功能,要使用其功能首先要开启RTX_HTTPServer服务. 2.阅读文档http://rtx.tencent.com/sdk/,为了 ...
- Uva1553 Caves and Tunnels LCT
简单题,主要为了练手. #include <cstdio> #include <iostream> #define maxn 100010 using namespace st ...
- 03-MyBatis主从实现代码读写分离应用以及实现
建立目录结构:
- PAT甲级1012. The Best Rank
PAT甲级1012. The Best Rank 题意: 为了评估我们第一年的CS专业学生的表现,我们只考虑他们的三个课程的成绩:C - C编程语言,M - 数学(微积分或线性代数)和E - 英语.同 ...
- JSoup 用法详解
清单 1 // 直接从字符串中输入 HTML 文档 String html = "<html><head><title> 开源中国社区 </titl ...