ACM-ICPC 2018 焦作赛区网络预赛 G. Give Candies 打表+指数循环节 or欧拉降幂(费马小定理) 一题多解

博客目录

原题

传送门

  •  26.61%
  •  1000ms
  •  65536K

There are NN children in kindergarten. Miss Li bought them NN 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)(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.

Input

The first line contains an integer TT, the number of test case.

The next TT lines, each contains an integer NN.

1 \le T \le 1001≤T≤100

1 \le N \le 10^{100000}1≤N≤10100000

Output

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

样例输入复制

1
4

样例输出复制

8

题目来源

ACM-ICPC 2018 焦作赛区网络预赛

思路

通过打表我们发现,结果是pow(2,n-1)

然后本题可以用两种解法,第一个是欧拉降幂,第二个是指数循环节。我用的第二种。

欧拉降幂只能用于对质数取模的情况(不然可能要用到中国剩余定理反正各种麻烦而且复杂度就飙升了),质数循环节则无所谓质数合数

指数循环节:

寻找循环节长度的代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll const mod=1e9+7;
int main(){// 1 2 4 8 16
    int i;
    f[1]=1;
    ll w=1;
    for(i=1;i<=mod;i++){
        w*=2;
        w%=mod;
        if(w==1){
            cout<<i<<endl;
            break;
        }
    }
}

输出是500000003

也就是每500000003循环一次,所以有:

2^n%mod=2^(n%500000003)

然后我们用大数运算计算下式:

 

fpow(2,n%500000004)

其中n%500000004是大数取模运算,fpow是快速幂

欧拉降幂(费马小定理):

由欧拉公式有:phi(p)=p-1,(p为质数)    pow(a,phi(p))%p==1

所以有:

pow(a,b)%p=pow(a,b%(p-1)+p-1)%p

AC代码(指数循环节)

#include<cstring>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
char s[110000];
ll divMod(char* ch,ll num)
{
    ll s = 0;
    for(int i=0; ch[i]!='\0'; i++)
        s = (s*10+ch[i]-'0')%num;
        s=((s-1+num)%num+num)%num;
    return s;
}
ll PowerMod(ll a, ll b, ll c)

{

    ll ans = 1;
    a = a % c;
    while(b>0)
    {
        if(b % 2 == 1)
            ans = (ans * a) % c;
        b = b/2;
        a = (a * a) % c;
    }
    return ans;

}
int main()
{
    int t;
    ll n;
    cin>>t;
    while(t--)
    {
        cin>>s;
        ll d=divMod(s,500000003);
        cout<<PowerMod(2,d,1000000007)<<endl;
    }
    return 0;
}

欧拉降幂(费马小定理)

#include<cstring>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
char s[110000];
ll divMod(char* ch,ll num)
{
    ll s = 0;
    for(int i=0; ch[i]!='\0'; i++)
        s = (s*10+ch[i]-'0')%num;
        s=((s-1+num)%num+num)%num;
    return s;
}
ll PowerMod(ll a, ll b, ll c)

{

    ll ans = 1;
    a = a % c;
    while(b>0)
    {
        if(b % 2 == 1)
            ans = (ans * a) % c;
        b = b/2;
        a = (a * a) % c;
    }
    return ans;

}
int main()
{
    int t;
    ll n;
    cin>>t;
    while(t--)
    {
        cin>>s;
        ll d=divMod(s,1000000007-1);
        cout<<PowerMod(2,d,1000000007)<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值