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. 调用 GetProcAddress 失败,在 ISAPI 筛选器 "C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" 上

    1.选择网站的ISAPI筛选器,设置ASP.NET的 aspnet_filter.dll右键恢复为父项 如果问题还未解决,执行第2步: 2.是否注册了asp.net,打开cmd运行:C:\Window ...

  2. C#对象转JSON字符串和JSON字符串转对象

    namespace Net.String.ConsoleApplication { using System; using System.Data; using System.Collections; ...

  3. 使用node.js抓取有路网图书信息(原创)

    之前写过使用python抓取有路网图书信息,见http://www.cnblogs.com/dyf6372/p/3529703.html. 最近想学习一下Node.js,所以想试试手,比较一下http ...

  4. 编写高性能 Web 应用程序的 10 个技巧

    使用 ASP.NET 编写 Web 应用程序的简单程度令人不敢相信.正因为如此简单,所以很多开发人员就不会花时间来设计其应用程序的结构,以获得更好的性能了.在本文中,我将讲述 10 个用于编写高性能 ...

  5. php调试利器 -- xdebug

    之前整理在印象笔记中,现在搬出来.分本地调试和远程调试.本文亲测通过并截图,有问题欢迎留言探讨. (参考网上多位前辈的博客,整理在笔记里忘记保存原文链接,这里无法贴出链接,望见谅)   # 痛处 一般 ...

  6. Spark机器学习 Day1 机器学习概述

    Spark机器学习 Day1 机器学习概述 今天主要讨论个问题:Spark机器学习的本质是什么,其内部构成到底是什么. 简单来说,机器学习是数据+算法. 数据 在Spark中做机器学习,肯定有数据来源 ...

  7. 解决pxe网络批量安装部署linux遇到的问题和解决方法

    解决“出现Unable to retrieve 192.168.0.100/var/www/html/images/install.img错误” 分析:我们必须了解这个错误出现在哪个阶段才能正确找到错 ...

  8. Have Fun with Numbers (大数)

    Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, wit ...

  9. 浅析nginx的负载均衡

    Nginx 的 HttpUpstreamModule 提供对后端(backend)服务器的简单负载均衡.一个最简单的 upstream 写法如下: upstream backend { server ...

  10. Perl内置变量速查表

    [ 文件句柄 ] $| 如果非零, 则在对当前选定的文件执行写或打印操作后强制清除缓冲区 $% 当前选中文件句柄的当前页码 $= 当前选中文件句柄的当前页面长度 $- 当前选中文件句柄的页面剩余长度 ...