HDU 4602 Partition (快速幂+思维)

Define f(n) as the number of ways to perform n in format of the sum of some positive integers. For instance, when n=4, we have 
  4=1+1+1+1 
  4=1+1+2 
  4=1+2+1 
  4=2+1+1 
  4=1+3 
  4=2+2 
  4=3+1 
  4=4 
totally 8 ways. Actually, we will have f(n)=2  (n-1) after observations. 
Given a pair of integers n and k, your task is to figure out how many times that the integer k occurs in such 2  (n-1) ways. In the example above, number 1 occurs for 12 times, while number 4 only occurs once. 
Input
The first line contains a single integer T(1≤T≤10000), indicating the number of test cases. 
Each test case contains two integers n and k(1≤n,k≤10  9). 
Output
Output the required answer modulo 10  9+7 for each test case, one per line.
Sample Input
2
4 2
5 5
Sample Output
5
1


题意就像上面说的,很直白,说说思路:

题中给的2^(n-1)很有用

首先先转化问题,求5中2的个数等于求4中1的个数,求5中3的个数等于求4中2的个数,等于求3中1的个数,所有问题都可以转化为求n中1的个数。

下面求n中1的个数:

先写出3的分解情况:(4种)

1 1 1

1 2 

2 1

3

再写出4的分解情况:(8种)

1 1 1 1

1 2 1

2 1 1

3 1

1 1 2

1 3

2 2

4

注意我写的顺序,前4行是在3的基础上末尾添1,后4行是在3的基础上末尾加1。

注意到4的情况是3的两倍,而且这样添加不会重复,所以这个思路是可行的。

那么找递推关系:

发现4的情况的前一半是3的情况加3的行数,即A(n)=A(n-1)+2^(n-2)........(没推完呢)

再看4的后一半情况,1的个数等于3中1的个数减去3中末尾是1的个数,由上述推的过程可看出每个数有一半情况末尾是1,

所以A(n)=A(n-1)-0.5*2^(n-2)

综合起来得到一个递推关系:

A(n)=2*A(n-1)+2^(n-3)

下面要考虑算法,打表的话数据太大,不能打,所以要推出方程的一般形式直接算,

中间队友推出一个式子:A(n)=2^n+n*2^(n-3)

但是.......式子是对的,带到递推式里也成立,但是........数据带进去不对!!!为什么???搞了半天还是没搞懂

后来另一个队友推出一个以A3为基础的关系式:An=2^(n-3)*A3+(n-3)*2^(n-3)

首先这个式子是对的,而且测试了很多样例也都对,但是一直WA,还是没搞懂为什么!!!

那么......换方法吧。

队友又推出一个以A2为基础的式子:An=2^(n-2)*A2+(n-2)*2^(n-3)

这回终于A了

这道题总的来说思维还是很不错的,但是中间数据可能有些问题,所以这题很神奇

(AC代码本来想用欧拉定理的A ^ B % C  =A ^( B % ψ(C)+ψ(C) ) % C,但是没用也过了)

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#define MOD 1000000007
typedef long long LL;
using namespace std;
LL qpow(LL m,LL n,LL k)
{
    LL b=1;
    while(n>0)
    {
        if(n&1)b=(b*m)%k;
        n=n>>1;
        m=(m*m)%k;
    }return b;
}
LL phi(LL n)
{
    LL i,rea=n;
    for(i=2;i*i<=n;i++)
    {
        if(n%i==0)
        {
            rea=rea-rea/i;
            while(n%i==0)n/=i;
        }
    }
    if(n>1)
        rea=rea-rea/n;
    return rea;
}
int main()
{
     LL n,k;
     int t;
     LL ph=phi(MOD);
     scanf("%d",&t);

     while(t--)
     {      LL ans;
         scanf("%lld%lld",&n,&k);
         if(n<k)ans=0;
         if(n==k)ans=1;
         else
         {
             LL a3=5;
             LL m=n-k+1;
            if(m==2)
             ans=2;
             else
             {
                 ans=((2*qpow(2,m-2,MOD)%MOD)+(m-2)*qpow(2,m-3,MOD)%MOD)%MOD;
             }

         }printf("%lld\n",ans);
     }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值