ZOJ 3924 Musical Notes

本文介绍了一种通过算法解析音乐曲谱的方法,旨在帮助用户计算特定曲谱的组成方式及其可能性数量。考虑到不同音符的长度及组合限制,文章详细阐述了如何利用递归搜索结合组合数学原理来解决这一问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

Besides his skills of playing Hearthstone, Bob is also insterested in learning piano in order to make himself more charming, of course, to Alice.

Now, Bob had transcribed a musical score for himself to practice. Unfortunately, he only copied the numbered musical notation, which use numbers 1 to 7 representing the musical notes(sung as do, re, mi...) and he forgot to mark the musical notes, which means he did't know the lengths of those notes. Bob remembered that the composition contains m bar and k quarter rest in the end of the composition. Each bar include 4 beats, and the length of quarter equals one beat. There are 5 other musical notes with differnt lengths——whole note, half note, quarter note, eighth note and sixteenth note. In our cases, the length of a whole note is equal to 4 beats, and the length of others are respectively equal to 2, 1, 1/2 and 1/4 beats in order.

Knowing m and k, he can calculate the whole length of those musical notes, l beats, which equals 4*m-k beats. Then he counted out n, the number of notes represented by number 1 to 7, with which he may find out how the composition is formed, namely finding out the length of each note. He thought there might be too many cases to form the composition, so he want you to find out the number of possible ways to form the composition.

Input

The first line of input contains an integer T (T ≤ 20) . T is the number of the cases. In the next T lines, there are three integer mkn, representing the number of bars, the number of quarter rests, and the number of musical notes.(1 ≤ m ≤ 10000, 0 ≤ k ≤ 3, m ≤ n ≤ m+13)

Output

The output contains T lines, and each line contain an answer to a case. The answers might be to large, you should output them modulo 1000000007. If there is no possible ways, output 0.

Sample Input

1
2 0 5

Sample Output

75
Hint

The number of whole, half, quarter, eighth, sixteenth notes can be:

0, 3, 2, 0, 0. In this situation we may find 10 ways to form the composition.

1, 0, 4, 0, 0. In this situation we may find 5 ways.

1, 1, 1, 2, 0. In this situation we may find 60 ways.


简化一下,题意就是说有5种节拍分别是4,2,1,0.5,0.25然后总共有n个,这些节拍的和是4*m-k

并且呢,n的范围是[m,m+13],问的是这样的组合方案有多少种,于是直接dfs找出全部的可能乘上组合数

因为直接求组合数比较麻烦,我是直接用全排列除掉各自的排列数得到答案的,因为要取余,所以事先算好

阶乘对于1e9+7的逆元,然后在dfs里面就能直接算答案了。

#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
typedef long long LL;
const int mod=1e9+7;
const int maxn=1e4+10;
int a[5]={16,8,4,2,1};
int b[5];
int T,n,k,m;
LL ans,f[maxn],c[maxn];

LL inv(LL x, LL m)  
{  
    if (x == 1) return x;  
    return inv(m % x, m)*(m - m / x) % m;  
}  

void init()
{
	c[0]=f[0]=1;
	for (int i=1;i<maxn;i++) 
	{
		c[i]=c[i-1]*i%mod;
		f[i]=inv(c[i],mod);
	}
}

void dfs(int x,int y,int z)
{
	if (x*a[z]<y) return;
	if (z==4)
	{
		if (x==y)
		{
			b[4]=x;
			LL temp=c[n];
			for (int i=0;i<5;i++) temp=temp*f[b[i]]%mod;
			(ans+=temp)%=mod;
		}
		return;
	}
	for (int i=0;i<=x;i++)
	{
		b[z]=i;
		dfs(x-i,y-a[z]*i,z+1);
	}
}

int main()
{
	init();
	scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d%d",&m,&k,&n);
		ans=0;
		dfs(n,16*m-4*k,0);
		printf("%lld\n",ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值