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; } }