HDU 4704 Sum(费马小定理,组合数学,快速幂)
来源:互联网 发布:淘宝网卖家体检中心 编辑:程序博客网 时间:2024/06/06 00:14
Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2738 Accepted Submission(s): 1140
Problem Description
Sample Input
2
Sample Output
2Hint1. For N = 2, S(1) = S(2) = 1.2. The input file consists of multiple test cases.
思想:根据组合数学,总是为2^n,根据费马小定理缩小n的范围,再矩阵快速幂即可求出答案。
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<queue>#include<stack>#include<vector>#include<cstring>#include<string>#include<algorithm>using namespace std;#define ms(a,b)memset(a,b,sizeof(a))#define eps 1e-10#define inf 1e8typedef long long ll;typedef vector<ll> vec;typedef vector<vec> mat;const ll M=1000000007;ll mod_pow(ll x,ll n){ ll ans=1; while(n>0) { if(n&1) ans=(ans*x)%M; x=x*x%M; n>>=1; } return ans;}int main(){ ll a,b,n; string s; ll ans=0; while(cin>>s) { ans=0; for(int i=0;i<s.length();i++) { ans=ans*10+s[i]-'0'; ans%=(M-1); } ans--; cout<<mod_pow(2,ans)<<endl; } return 0;}
JAVA:(TLE)
import java.math.BigInteger;import java.util.Scanner;public class Main {public static void main(String[] args) {BigInteger n;Scanner cin=new Scanner(System.in);while(cin.hasNextBigInteger()){n=cin.nextBigInteger();n=n.subtract(BigInteger.ONE);n=n.mod(new BigInteger("1000000006"));System.out.println(mod_pow(new BigInteger("2"),n,new BigInteger("1000000007")));}}public static BigInteger mod_pow(BigInteger x,BigInteger n,BigInteger mod){BigInteger res=new BigInteger("1");while(n.compareTo(BigInteger.ZERO)>0){if(n.mod(new BigInteger("2")).compareTo(BigInteger.ONE)==0){res=res.multiply(x);res=res.mod(mod);}x=x.multiply(x);x=x.mod(mod);n=n.divide(new BigInteger("2"));}return res;}}
阅读全文
0 0
- HDU 4704 Sum(费马小定理,组合数学,快速幂)
- HDU 4704 Sum 费马小定理+快速幂
- HDU 4704Sum(费马小定理+快速幂)
- HDU 4704 Sum(快速幂+费马小定理)
- hdu 4704 Sum (费马小定理+快速幂)
- HDU--4704 Sum【费马小定理,快速幂】
- HDU - 4704 Sum (费马小定理 + 快速幂)
- hdu 4704 sum(费马小定理+快速幂)
- [HDU 4704] Sum · 费马小定理 & 快速幂
- HDU 4704 SUM 整数快速幂+费马小定理
- 数论 --- 费马小定理 + 快速幂 HDU 4704 Sum
- HDU 4704 Sum 【隔板原理+费马小定理+快速幂】
- HDU4869 组合数学+费马小定理求逆元+快速幂
- hdu 5651 组合数学+费马小定理求逆元
- HDOJ Sum 4704【费马小定理+快速幂】
- hdoj 4704 Sum 【费马小定理 + 快速幂】
- hdu 5106 同余定理+组合数学+快速幂
- HDU 4704 Sum (费马定理+快速幂)
- ActionBar标题居中
- java.lang.OutOfMemoryError: Java heap space解决方法
- angularjs数据请求、分页问题
- 在VC2015里包含了lib库,但没有设置对路径的出错
- RGB565转换为RGB88,并作线性补偿
- HDU 4704 Sum(费马小定理,组合数学,快速幂)
- leetcode--evaluate-reverse-polish-notation
- 我的C++学习之路
- 多线程问题总结
- Oracle 导出导入数据
- 四、组件化和属性(props)
- IOS IJKPlayer播放RTSP
- Xshell连接Linux
- Android 7.0 自定义系统service过程与注意事项