sum 大数取余+快速幂

Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 3980    Accepted Submission(s): 1620

Problem Description

Sample Input

2

Sample Output

2

Hint

1. For N = 2, S(1) = S(2) = 1.

2. The input file consists of multiple test cases.

Source

2013 Multi-University Training Contest 10

Recommend

zhuyuanchen520

一道学长教的数论题。找出规律后发现求的是2^(n-1)。由于n非常大,所以在输入的时候可以分位取余(((a*100%MOD)+b*10%NOD)+c%MOD)%MOD,最后跑一边快速幂。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<math.h>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#define MAX 1000005
#define INF 0x3f3f3f3f
#define MOD 1000000007
using namespace std;

typedef long long ll;

ll qMod(ll a,ll b){
    ll ans=1;
    while(b>0){
        if(b&1) ans=ans*a%MOD;
        b>>=1;
        a=a*a%MOD;
    }
    return ans;
}
int main()
{
    int n,len,i;
    char s[MAX];
    while(~scanf(" %s",s)){
        ll c=0;
        len=strlen(s);
        for(i=0;i<len;i++){
            int x=s[i]-‘0‘;
            c=c*10+x;
            if(c>1000000006) c%=1000000006;
        }
        c=((c-1)%1000000006+1000000006)%1000000006;
        printf("%lld\n",qMod(2,c));
    }
    return 0;
}

原文地址:https://www.cnblogs.com/yzm10/p/8748094.html

时间: 2024-10-12 05:56:11

sum 大数取余+快速幂的相关文章

1214 - Large Division -- LightOj(大数取余)

http://lightoj.com/volume_showproblem.php?problem=1214 这就是一道简单的大数取余. 还想还用到了同余定理: 所谓的同余,顾名思义,就是许多的数被一个数d去除,有相同的余数.d数学上的称谓为模.如a=6,b=1,d=5,则我们说a和b是模d同余的.因为他们都有相同的余数1. //// 数学上的记法为: a≡ b(mod d) 可以看出当n<d的时候,所有的n都对d同商,比如时钟上的小时数,都小于12,所以小时数都是模12的同商. 对于同余有三种

POJ 2447 RSA 大数分解+逆元+快速幂

链接:http://poj.org/problem?id=2447 题意: 思路:Pollard_Rho质数分解,得到两个素数因子,P,Q,求出T,E,快速幂即可得M. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <map> #include <cstdlib> #include <queue>

Raising Modulo Numbers 取模+快速幂

Raising Modulo Numbers 题目地址:http://poj.org/problem?id=1995 Description People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in their cellar, others like using Windows, and some like difficult

LightOJ 1214 - Large Division (大数取余)

1214 - Large Division PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 32 MB Given two integers, a and b, you should checkwhether a is divisible by b or not. We know that an integer ais divisible by an integer b if and only if the

HDU 5832 A water problem 大数取余

A water problem Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 650    Accepted Submission(s): 333 Problem Description Two planets named Haha and Xixi in the universe and they were created with

hdoj-1212-Big Number【大数取余&amp;简单题】

Big Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6015 Accepted Submission(s): 4205 Problem Description As we know, Big Number is always troublesome. But it's really important in our ACM.

FZU 2108(dfs模拟,大数取余)

K Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice FZU 2108 Description Given one non-negative integer A and one positive integer B, it’s very easy for us to calculate A Mod B. Here A Mod B means the

快速幂+大数取模

快速幂+大数取模 快速幂,其实就是求(a^b)% p,(其中a,b,p都比较大在int范围内)这类问题. 首先要知道取余的公式:(a*b)%p=(a%p*b%p)%p. 那么幂不就是乘机的累积吗,由此给出代码: int fast(int a,int b,int p) {   long long a1=a,t=1; while(b>0) { if(b&1)          /如果幂b是奇数多乘一次,因为后边会除2变偶数,(7/2=3) t=(t%p)*(a1%p)%p; a1=(a1%p)*

快速幂、快速乘

在ACM的比赛中,我们经常会遇到指数型的数据的取模问题. 如果我们直接对数据进行取模,由于题目所给的数据的范围很大,会导致爆int 或者 long long 所以我们要采取快速幂取模 先看一组例子: 2*2*2*2*2*2*2*2*2*2*2 我们可以这样去算 原式=4*4*4*4*4*2 =8*8*4*2 =16*4*2 就是先合并后计算,这样就可以减少计算的步骤 快速幂的基本理论: 积的取余等于取余的积取余 快速幂就是我们先不断的降低a,b的规模,然后再进行计算 a^b 降低a的规模: 只要