1. 程式人生 > >ACM-ICPC 2018 焦作賽區網路預賽 -G Give Candies(擴充套件尤拉定理+大數取模)

ACM-ICPC 2018 焦作賽區網路預賽 -G Give Candies(擴充套件尤拉定理+大數取模)

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 N.

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

題目來源

題解:

由題目可推出:ans=2^n %1000000007  % 1000000007;毫無疑問只是一道大數取模的題

由尤拉公式可知    gcd(2,1000000007)=1;所以:ans=2^n %1000000007  % 1000000007=2^(n % φ(1000000007));

φ(1000000007)=1000000006;  

由余數定理可知:(a+b)%d=(a%d+b%d)%d    a*b%d=(a%d*b%d)%d;

所以n可以用字串存取然後每次乘10相加  取模就好。

#include<cstring>
#include<cstdio>
#include<iostream>
#include<string>
#include<queue>
#include<vector>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<functional>
using namespace std;
#define clr(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&-x
#define rep(a,b,c) for(ll a=b;a<c;a++)
#define dec(a,b,c) for(int a=b;a>c;a--)
#define eb(x) emplace_back(x)
#define pb(x) push_back(x)
#define ps(x) push(x)
#define MAX_N 100000+5
#define MAX_M 100
typedef long long ll;
typedef unsigned long long ull;
typedef priority_queue<ll,vector<ll>,greater<ll> >pqg;
const ll maxn=1000+5;
const ll inf=1e7;
ll mod=1e9+7;
int n,m,k;
ll room[maxn];
ll dpmax[maxn][6],dpmin[maxn][6];
char f[7];
ll solve()
{
    ll ans=-inf;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m&&j<=i;j++)
        {
            ll tmax=dpmax[i-1][j-1],tmin=dpmin[i-1][j-1];
            ll maxx,minn;
            switch(f[j])
            {
            case '+':
                {
                    maxx=tmax+room[i];
                    minn=tmin+room[i];
                    break;
                }
            case '-':
                {
                    maxx=tmax-room[i];
                    minn=tmin-room[i];
                    break;
                }
            case '*':
                {
                    maxx=max(tmax*room[i],tmin*room[i]);
                    minn=min(tmin*room[i],tmax*room[i]);
                    break;
                }
            case '/':
                {
                    maxx=max(tmax/room[i],tmin/room[i]);
                    minn=min(tmin/room[i],tmax/room[i]);
                    break;
                }
            }
            dpmax[i][j]=max(dpmax[i-1][j],maxx);
            dpmin[i][j]=min(dpmin[i-1][j],minn);
            if(i==j){dpmax[i][j]=maxx;dpmin[i][j]=minn;}
        }
    }
    return dpmax[n][m];
}
int main()
{
#ifndef ONLINE_JUDGE
    // freopen("data.txt","r",stdin);
#endif
    int t;
    scanf("%d",&t);
    while(t--)
    {
      clr(dpmax,0);
      clr(dpmin,0);
      scanf("%d%d%d",&n,&m,&k);
      for(int i=1;i<=n;i++)
      {
          scanf("%lld",&room[i]);
          dpmin[i][0]=dpmax[i][0]=k;
      }
      dpmin[0][0]=dpmax[0][0]=k;
      cin>>f+1;
      printf("%lld\n",solve());
    }
    return 0;
}