1. 程式人生 > >ACM-ICPC 2018 焦作賽區網路預賽G Give Candies

ACM-ICPC 2018 焦作賽區網路預賽G Give Candies

題意:給你n個東西,叫你把n分成任意段,這樣的分法有幾種。

分析:(HDU 4704原題)隔板法,ans=C(1,n-1)+C(2,n-1)+...+C(n-1,n-1)=2^(n-1)。需要用到尤拉降冪公式,參考開啟,證明需要用到尤拉定理,a^(φ(m))同餘1(mod m) (a與m互質)。

程式碼:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#pragma comment(linker, "/STACK:102400000,102400000")
#include<unordered_map>
#include<unordered_set>
#include<algorithm>
#include<iostream>
#include<fstream>
#include<complex>
#include<cstdlib>
#include<cstring>
#include<cassert>
#include<iomanip>
#include<string>
#include<cstdio>
#include<bitset>
#include<vector>
#include<cctype>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<deque>
#include<list>
#include<set>
#include<map>
using namespace std;
#define pt(a) cout<<a<<endl
#define debug test
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define inf 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
typedef pair<int,int> PII;
const ll mod = 1e9+7;
const int N = 1e6+10;

ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qp(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
int to[4][2]={{-1,0},{1,0},{0,-1},{0,1}};

int t;
string s;

int main() {
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>t;
    while(t--) {
        cin>>s;
        int l=s.size();
        ll res=0,phi=mod-1;
        rep(i,0,l-1) res=(res*10+s[i]-'0')%phi;
        cout<<qp(2,res-1+phi)<<endl;
    }
    return 0;
}