hdu4602(快速幂)
来源:互联网 发布:程序员的统计学 编辑:程序博客网 时间:2024/05/29 16:45
Partition
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 258 Accepted Submission(s): 127
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本题在比赛的时候出答案好快,但一时又想不出缘由,于是找规律1,2,5,12,28,64·······1,2,2*2+2^0,2*5+2^1,2*12+2^2······于是想到递推式:a[0]=1,a[1]=2a[i]=2*a[i-1]+2^(i-2),i>1,i=n-k由于1≤n,k≤109很大,直到递推式还不能解决问题,需要得到通式a[0]=1,a[1]=2a[n-k]=(n-k+3)*2^(n-k-2),n-k>1由于n-k-2可能很大,需要用快速幂计算#include<cstdio>#include<iostream>using namespace std;const __int64 mod=1000000000+7;__int64 optimized_pow_n(__int64 x, __int64 n)//快速幂x^n{ __int64 pw = 1; while (n > 0){ if (n&1) pw *= x; pw=pw%mod; x*=x; x=x%mod; n>>= 1; } return pw%mod;}int main(){int t,n,k;cin>>t;while(t--){scanf("%d%d",&n,&k);if(k>n)printf("0\n");else if(n==k)printf("1\n");else if(n-1==k)printf("2\n");else {printf("%I64d\n",(3+n-k)*optimized_pow_n(2,(__int64)(n-k-2))%mod);}} return 0;}
- hdu4602(快速幂)
- hdu4602 Partition 快速幂
- hdu4602
- hdu4602
- 常见的求通项公式类型 多校 hdu4602 二分法求快速幂
- [HDU4602]Partition
- hdu4602: Partition
- hdu4602 - Partition(公式推导)
- HDU4602+数学+推导公式
- HDU4602--Partition HDU(138)
- 概率难题-hdu4602-隔板法,暴力
- 快速矩阵快速幂
- 快速幂,快速幂取模
- 快速幂 + 快速幂取模
- 快速幂 快速幂取模
- 快速幂&&快速乘法
- 快速幂&快速乘
- 快速幂 快速乘法
- 云计算之路
- 自定义类加载器
- NALU(NAL单元)的顺序要求
- C# SortedDictionary<TKey,TValue>排序 用法 Sort()用法
- Codeforces Round #192 (Div. 2) E. Graph Reconstruction (随机算法)
- hdu4602(快速幂)
- java第四节课堂笔记(类与对象,static和this)
- KVM虚拟机代码揭秘——QEMU代码结构分析
- Code Fragment-提供可选选项的接口胜于单一接口
- Unity 异步加载场景
- python的文件操作file:(内置函数,如seek、truncate函数)
- N皇后问题
- VB中的类模块
- 栈回溯(stack trace)原理