zoukankan      html  css  js  c++  java
  • 大数的幂

    问题 G: Give Candies

    时间限制: 1 Sec  内存限制: 128 MB
    提交: 258  解决: 101
    [提交] [状态] [命题人:admin]

    题目描述

    There are N children in kindergarten. Miss Li bought them N candies。To make the process more interesting, Miss Li comes up with the rule: All the children line up according to their student number (1…N), and each time a child is invited, Miss Li randomly gives him some candies (at least one). The process goes on until there is no candy. Miss Li wants to know how many possible different distribution results are there. 

    输入

    The first line contains an integer T, the number of test case.
    The next T lines, each contains an integer N.
    1 ≤ T ≤ 100
    1 ≤ N ≤ 10^100000
     

    输出

    For each test case output the number of possible results (mod 1000000007).

    样例输入

    1
    4
    

    样例输出

    8
    

    思路:这题只要计算出2的n-1次方即可

    费马小定理 : 如果mod为质数,(a^n )%mod =  a^(n%(mod-1)) %mod

    #include <iostream>
    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int mod=1e9+7;
    const int maxn = 1e6+10;
    char  a[maxn];
    ll qpow(ll a,ll b)
    {
        ll ret=1;
        while(b)
        {
            if (b&1) ret=ret*a%mod;
            a=a*a%mod;
            b>>=1;
        }
        return ret;
    }
    int main()
    {
        int T,len;
        cin>>T;
        while(T--)
        {
            ll sum=0;
            scanf("%s",a);
            len=strlen(a);
            for (int i=0;i<len;i++)
            {
                sum=(sum*10+a[i]-48)%(mod-1);
            }
            if (sum==0) sum=mod-1;
            ll ans=qpow(2,sum-1);
            printf("%lld
    ",ans);
        }
        return 0;
    }

    其实这个题也可以使用欧拉降幂,这个公式其实是费马小定理的推广,费马小定理是欧拉定理的一种特殊情况。

    欧拉函数即是1-n与n互质的数的个数,指数的欧拉函数当然为本身-1

    欧拉降幂公式:

    (a^b)%m=a^(b%phi(m))%m

    贴一份队友的代码

    #include<bits/stdc++.h>
    using namespace std;
     
    typedef long long ll;
    const ll mod=1e9+7;
    string s;
     
    inline ll phi(int n)
    {
        int ans=n,temp=n;
        for (int i=2; i*i<=temp;i++)
        {
            if (temp%i==0)
            {
                ans-=ans/i;
                while(temp%i==0) temp/=i;
            }
        }
        if (temp>1) ans-=ans/temp;
        return ans;
    }
    inline ll qpow(ll x,ll n,ll mod)
    {
        ll ans=1;
        while(n)
        {
            if (n&1) ans=ans*x%mod;
            x=x*x%mod;
            n/=2;
        }
        return ans;
    }
     
    int main()
    {
        int T;
        ll phic=phi(mod);
        cin>>T;
        while(T--)
        {
            cin>>s;
            int i,len=s.size();
            ll res=0,ans;
            for (i=0;i<len;i++)
            {
                res=res*10+s[i]-48;
                if (res>phic)
                    break;
            }
            if (i==len)
                ans=qpow(2,res-1,mod)%mod;
            else
            {
                res=0;
                for (int i=0;i<len;i++)
                {
                    res=res*10+s[i]-48;
                    res%=phic;
                }
                ans=qpow(2,res+phic-1,mod)%mod;
            }
            cout<<ans<<endl;
        }
        return 0;
    }

  • 相关阅读:
    Django--URLconf
    Django--视图层
    Django 初识之安装下载以及模型目录简介
    jQuery基础
    BOM&DOM
    带你快速上手前端三剑客之css
    mysql 触发器、流程控制、事务等
    前端之html
    python中pymsql常用方法(1)
    18、设计模式-行为型模式-迭代器模式
  • 原文地址:https://www.cnblogs.com/ztdf123/p/11325438.html
Copyright © 2011-2022 走看看