1. 程式人生 > >ACM-ICPC 2018 焦作賽區網路預賽 G. Give Candies 打表+指數迴圈節 or尤拉降冪 一題多解

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

題目來源

思路

通過打表我們發現,結果是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是快速冪

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; }