您的位置:首页 > 其它

【HDOJ】4373 Mysterious For

2016-02-21 15:01 323 查看
1. 题目描述
有两种不同类型的循环,并给出一个由1、2组成的序列,表示嵌套的循环类型。
问这样组着的循环一共需要多少次循环?并将结果模364875103。

2.基本思路
显然,每当遇到一个类型1的序列,即可以判定12...2的嵌套循环共多少次,而1类型的循环次数为常亮。
因此,将原序列从1分开,并将每个子序列的循环次数相乘即为总的循环次数。
1 共循环n次 = C
[1]
12 共循环n*(n+1)/2次 = C[n+1][2]
122 共循环n*(n+1)*(n+2)/6次 = C[n+2][3]
12..2 |2|=m 共循环n*(n+1)*(n+2)/6次 = C[n+m-1][m]
由Lucas定理可知,假设p为素数,有



比较悲剧的是364875103是个合数,将它拆解为两个素数并使用LUCAS后,
我们可以知道ans=a1(mod p1), ans=a2(mod p2), 且p1, p2互素,求ans = ? (mod p1*p2)。
使用中国剩余定理。
使用欧拉定理求逆。

3. 代码

/* 4373 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000")

#define sti                set<int>
#define stpii            set<pair<int, int> >
#define mpii            map<int,int>
#define vi                vector<int>
#define pii                pair<int,int>
#define vpii            vector<pair<int,int> >
#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 clr                clear
#define pb                 push_back
#define mp                 make_pair
#define fir                first
#define sec                second
#define all(x)             (x).begin(),(x).end()
#define SZ(x)             ((int)(x).size())
#define lson            l, mid, rt<<1
#define rson            mid+1, r, rt<<1|1
#define LL                __int64

const int maxn = 25;
const int mod1 = 97;
const int mod2 = 3761599;
const int mod = 364875103;
LL fact1[mod1+15];
LL fact2[mod2+15];
LL e1, e2;
int pos[maxn];
int n, m, k;

LL Pow(LL base, LL n, LL mod) {
LL ret = 1;

base %= mod;
while (n) {
if (n & 1)
ret = ret * base % mod;
base = base * base % mod;
n >>= 1;
}

return ret;
}

void init() {
fact1[0] = fact2[0] = 1;
rep(i, 1, mod1)
fact1[i] = fact1[i-1] * i % mod1;
rep(i, 1, mod2)
fact2[i] = fact2[i-1] * i % mod2;
e1 = mod2 * Pow(mod2, mod1-2, mod1);
e2 = mod1 * Pow(mod1, mod2-2, mod2);

#ifndef ONLINE_JUDGE
printf("e1 = %I64d, e2 = %I64d\n", e1, e2);
#endif
}

LL C(LL n, LL m, LL mod, LL *fact) {
if (n < m)    return 0;
return fact
* Pow(fact[m]*fact[n-m], mod-2, mod) % mod;
}

LL Lucas(LL n, LL m, LL mod, LL *fact) {
if (m == 0)    return 1;
return C(n%mod, m%mod, mod, fact) * Lucas(n/mod, m/mod, mod, fact);
}

void solve() {
LL ans = 1, tmp;
LL a1, a2;

rep(i, 0, k) {
m = pos[i+1]-pos[i];
a1 = Lucas(n+m-1, m, mod1, fact1);
a2 = Lucas(n+m-1, m, mod2, fact2);
tmp = (a1*e1 + a2*e2) % mod;
#ifndef ONLINE_JUDGE
printf("a1 = %I64d, a2=%I64d, tmp=%I64d\n", a1, a2, tmp);
#endif
ans = ans * tmp % mod;
}

printf("%I64d\n", ans);
}

int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif

int t;

init();
scanf("%d", &t);
rep(tt, 1, t+1) {
scanf("%d%d%d", &n, &m, &k);
rep(i, 0, k)
scanf("%d", &pos[i]);
pos[k] = m;
printf("Case #%d: ", tt);
solve();
}

#ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif

return 0;
}


4. 数据生成器

from copy import deepcopy
from random import randint, shuffle
import shutil
import string

def GenDataIn():
with open("data.in", "w") as fout:
t = 20
bound = 10**5
fout.write("%d\n" % (t))
for tt in xrange(t):
n = randint(20, bound)
m = randint(20, bound)
k = randint(1, 15)
fout.write("%d %d\n%d " % (n, m, k))
L = [0]
st = set()
for i in xrange(1, k):
while True:
x = randint(1, m)
if x not in st:
break
L.append(x)
st.add(x)
L = sorted(L)
fout.write(" ".join(map(str, L)) + "\n")

def MovDataIn():
desFileName = "F:\eclipse_prj\workspace\hdoj\data.in"
shutil.copyfile("data.in", desFileName)

if __name__ == "__main__":
GenDataIn()
MovDataIn()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: