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

2.基本思路
显然,每当遇到一个类型1的序列,即可以判定12...2的嵌套循环共多少次,而1类型的循环次数为常亮。
因此,将原序列从1分开,并将每个子序列的循环次数相乘即为总的循环次数。
1     共循环n次 = C[n][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 = ;
const int mod1 = ;
const int mod2 = ;
const int mod = ;
LL fact1[mod1+];
LL fact2[mod2+];
LL e1, e2;
int pos[maxn];
int n, m, k; LL Pow(LL base, LL n, LL mod) {
LL ret = ; base %= mod;
while (n) {
if (n & )
ret = ret * base % mod;
base = base * base % mod;
n >>= ;
} return ret;
} void init() {
fact1[] = fact2[] = ;
rep(i, , mod1)
fact1[i] = fact1[i-] * i % mod1;
rep(i, , mod2)
fact2[i] = fact2[i-] * i % mod2;
e1 = mod2 * Pow(mod2, mod1-, mod1);
e2 = mod1 * Pow(mod1, mod2-, 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 ;
return fact[n] * Pow(fact[m]*fact[n-m], mod-, mod) % mod;
} LL Lucas(LL n, LL m, LL mod, LL *fact) {
if (m == ) return ;
return C(n%mod, m%mod, mod, fact) * Lucas(n/mod, m/mod, mod, fact);
} void solve() {
LL ans = , tmp;
LL a1, a2; rep(i, , k) {
m = pos[i+]-pos[i];
a1 = Lucas(n+m-, m, mod1, fact1);
a2 = Lucas(n+m-, 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, , t+) {
scanf("%d%d%d", &n, &m, &k);
rep(i, , k)
scanf("%d", &pos[i]);
pos[k] = m;
printf("Case #%d: ", tt);
solve();
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

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()

【HDOJ】4373 Mysterious For的更多相关文章

  1. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

  2. 【HDOJ】【3506】Monkey Party

    DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...

  3. 【HDOJ】【3516】Tree Construction

    DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...

  4. 【HDOJ】【3480】Division

    DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...

  5. 【HDOJ】【2829】Lawrence

    DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...

  6. 【HDOJ】【3415】Max Sum of Max-K-sub-sequence

    DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...

  7. 【HDOJ】【3530】Subsequence

    DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...

  8. 【HDOJ】【3068】最长回文

    Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...

  9. 【HDOJ】【1512】Monkey King

    数据结构/可并堆 啊……换换脑子就看了看数据结构……看了一下左偏树和斜堆,鉴于左偏树不像斜堆可能退化就写了个左偏树. 左偏树介绍:http://www.cnblogs.com/crazyac/arti ...

随机推荐

  1. WCF、Web API、WCF REST、Web Service之区别

    http://www.dotnet-tricks.com/Tutorial/webapi/JI2X050413-Difference-between-WCF-and-Web-API-and-WCF-R ...

  2. $.ligerDialog 操作

    //关闭 $.ligerDialog.open 打开的弹窗 frameElement.dialog.close(); //关闭父窗口 parent.$.ligerDialog.close(); //关 ...

  3. epoll_create, epoll_ctl和epoll_wait

    参考代码 #include <iostream> #include <sys/socket.h> #include <sys/epoll.h> #include & ...

  4. 【原创】一起学C++ 之指针、数组、指针算术 ---------C++ primer plus(第6版)

    C++ Primer Plus 第6版 指针和数组基本等价的原因在于指针算术! 一.指针 ⑴整数变量+1后,其值将增加1: ⑵指针变量+1后,增加的量等于它指向的类型的字节数: ⑶C++将数组名解析为 ...

  5. C中浮点数转字符串

    求浮点数转换成字符串,如何才能获得比较正确的字符串.用printf("%f\n", (float)5); 这种方式转换出来的结果是 5.000000 ,末尾都会带6位小数. 控制精 ...

  6. (转)Qt Model/View 学习笔记 (七)——Delegate类

    Qt Model/View 学习笔记 (七) Delegate  类 概念 与MVC模式不同,model/view结构没有用于与用户交互的完全独立的组件.一般来讲, view负责把数据展示 给用户,也 ...

  7. GNU_makefile_template

    #g++ compiler: options # -std=c++0x enables ISO C++ 11 standard # -I.. pulls in the Version_test.h f ...

  8. 【学习总结】UIGestureRecognizer(手势识别器)

    基本知识点 : -> IOS 3.2之后 , 苹果推出了手势识别功能 ( Gesture Recognizer ) 在触摸事件处理方面 , 简化开发难度. -> UIGesture Rec ...

  9. .net google calendar

    https://developers.google.com/gdata/client-cs http://www.codeproject.com/Articles/64474/How-to-Read- ...

  10. crontab定时任务中文乱码问题

    手动执行都很正常的的脚本,添加到定时任务中日志文件全是乱码经过多方查证终于找到了原因! crontab启动的任务没有获取系统的环境变量,导致中文乱码解决办法:   在执行的脚步中添加编码方式或者添加对 ...