HDOJ 题目4602 Partition(找规律,快速幂)
来源:互联网 发布:pic单片机eeprom 编辑:程序博客网 时间:2024/05/04 09:29
Partition
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2635 Accepted Submission(s): 1052
Problem Description
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.
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≤109).
Each test case contains two integers n and k(1≤n,k≤109).
Output
Output the required answer modulo 109+7 for each test case, one per line.
Sample Input
24 25 5
Sample Output
51
Source
2013 Multi-University Training Contest 1
Recommend
liuyiding | We have carefully selected several similar problems for you: 5193 5192 5191 5190 5189
1 2 3 4 5
1 1 2 5 12 28
2 1 2 5 12
3 1 2 5
4 1 2
5 1
规律:2^(m-3)*(m-2). m=n-k+1
ac代码
#include<stdio.h>#include<string.h>#define mod 1000000007__int64 qpow(__int64 a,__int64 b){__int64 ans=1;while(b){if(b&1)ans=(ans*a)%mod;a=(a*a)%mod;b/=2;}return ans;}int main(){int t;scanf("%d",&t);while(t--){__int64 n,k;scanf("%I64d%I64d",&n,&k);if(k>n){printf("0\n");continue;}if(n-k+1<=2){printf("%d\n",n-k+1);}else{printf("%I64d\n",(qpow(2,n-k-2)*(n-k+3))%mod);}}}
0 0
- HDOJ 题目4602 Partition(找规律,快速幂)
- HDU 4602 Partition (整数拆分&找规律&快速幂取模)
- HDU 4602 Partition 组成n的方案中k有几个 (好题!!!!找规律题+快速幂)
- hdu 4602 Partition (找规律)
- HDU-4602 partition 找规律
- HDOJ 题目4349 Xiao Ming's Hope(找规律)
- 【矩阵快速幂-找规律】HDOJ Reading comprehension 4990
- hdu6198(矩阵快速幂+找规律)
- HDOJ 题目1165 Eddy's research II(递推,找规律)
- (精)hdoj(找规律)2374
- HDOJ 2674 N!Again(找规律)
- hdoj LCP Array 5635 (找规律)
- HDOJ 1021 Fibonacci Again(找规律)
- HDOJ 1443 joseph (找规律)
- hdoj 1005 number sequence(找规律)
- Hdoj 1021 Fibonacci Again (找规律)
- hdoj 1046 Gridland(找规律)
- hdoj 5344 找规律
- 百度地图API的使用——网页中写入百度地图
- LeetCode OJ String to Integer (atoi)
- [iPhone高级] 基于XMPP的IOS聊天客户端程序(XMPP服务器架构)
- #个人赛第六场解题总结#
- JAVA学习笔记(八)- 字符串类和方法
- HDOJ 题目4602 Partition(找规律,快速幂)
- C语言中的位移运算
- 第三方支付平台-框架介绍
- LeetCode OJ ZigZag Conversion
- 关于jsp内置对象Session和Cookie使用的肤浅理解,如何使用Session来统计网站在线人数
- 子类对基类数组的引用
- LeetCode OJ Longest Substring Without Repeating Characters
- 链表在第几个位置添加和删除
- LeetCode OJ Valid Sudoku