HDU 4062 Partition

來源:互聯網
上載者:User
Partition

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1049 Accepted Submission(s): 427

Problem DescriptionDefine 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.

InputThe 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≤109).

OutputOutput the required answer modulo 109+7 for each test case, one per line.

Sample Input
24 25 5

Sample Output
51

 思路:

把1~4的每個元素的個數列出來後就可以簡單看出其中的固定規律

   1 2 3  4

 11 2 5 12

2  1 2 5

3    1 2

4       1

 

所以數列符合a_1 = 1, 2, 5, 12 。。。。。a_n = 2*f(n-1)+2^(n-3)

 

最後問題推匯出的公式:a_n = 2^(n-1) + (n-2)*2^(n-3);

 

 

 

import java.io.BufferedInputStream;import java.util.*;public class Main {public static long t1=1000000000+7;public static void main(String[] args) {Scanner sc = new Scanner(new BufferedInputStream(System.in));int t = sc.nextInt();for (int i = 0; i < t; i++) {int n = sc.nextInt();int k = sc.nextInt();if (k > n) {System.out.println("0");} else {int b = n - k + 1;if (b == 1)System.out.println("1");else if (b == 2)System.out.println("2");else if (b == 3)System.out.println("5");else {long num = (power(2,b-1)%t1  + (b - 2) * power(2,b-3))%t1 ;num%=t1;System.out.println(num);}}}} public static long power(int a,int n)  {      if (n==0) return 1;      if (n==1) return a;       long z=power(a,n/a);      if (n%2==0)          return z*z%t1;      else return z*z*a%t1;  }  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.