C

来源:互联网 发布:电子白板课件制作软件 编辑:程序博客网 时间:2024/06/05 08:44
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
24 25 5
Sample Output
5

1

比赛的时候队友推出了公式,然后直接快速幂,

因为你会发现所有的数其实是同一数列的数,只要求出数列的首项和通项就可以了,首项明显是1,

经推到通项是an=2^(n-3)*(n+2)(n>3)

ac代码:

#include <iostream>#include <stdio.h>#include <stdlib.h>#include <math.h>#include <string.h>using namespace std;const unsigned long long mod=1000000007;long long quickmod(long long a,long long b){    long long ans=1;    while(b>0)    {        if(b%2==1)            ans=ans*a%mod;        a=(a*a)%mod;        b/=2;    }    return ans%mod;}int main(){    long long n,k;    int t;    scanf("%d",&t);    while(t--)    {    scanf("%lld%lld",&n,&k);    long long x=n-k+1;    long long jie=quickmod(2,x-1)%mod;    long long y=(jie%mod+(x-2)*quickmod(2,x-3)%mod)%mod;    if(n<k)        {printf("0\n");        continue;        }        if(n==k)            printf("1\n");        else        printf("%lld\n",y);    }    return 0;}

0 0
原创粉丝点击