title: woj1010 alternate sum 数学

date: 2020-03-10

categories: acm

tags: [acm,woj,数学]

一道数学题。简单。

题意

给一个集合Q,对于每个子集中元素非降序排列后,计算a1-a2+a3-a4....=alternate sum。

比如 Q={2,1,3,4},其本身alternate sum=4-3+2-1=2,而其中一个子集{1,3,4}:4-3+1=2;

求Q中所有子集的alternate sum之和。

输入格式

There are several test cases. For each test case, it contains:

Line 1: One integer N (1<=N<=1000) which specifies the size of set Q

Line 2: N integers (you are ensured that the absolute value of each integer is less than 2^16) which specify the elements of set Q.

There is a blank between two consecutive integers.

The input will be ended by zero.

输出格式

The answer of the total value may be very large, so you should divide this answer by 2006 and just output the remainder instead.

样例输入

1

-1

2

0 4

3

1 3 2

0

样例输出

2005

8

12

分析:这种一看是数学题,就在纸上算算。

一共有2^N个子集,单个计算是不现实的。

对于集合中最大元素maxn,包含它的子集有2^(N-1)个。其在计算中必然为 + 。有2^(N-1)个

对于第二大元素a2:包含它的子集一共有2^(N-1)个,其中一半有maxn,在计算时-a2,一半+a2。所以总的下来没影响。

其他数以此类推。

所以最后结果为maxn*2(N-1)

要说怎么想到的:学集合的时候知道子集数量是2N,其中2就相当于每个数取不取,然后就想到2(N-1)...

code

#include<iostream>
#include<cstdio>
using namespace std; int maxn,n,ans,num,i;
int main(){
while(scanf("%d",&num)==1&&num){
cin>>n;
maxn=n;
for(i=0;i<num-1;i++){
cin>>n;
maxn=(maxn>n)?maxn:n;
}
maxn=(maxn%2006+2006)%2006; //保证是正数
for(i=0;i<num-1;i++){
maxn=maxn*2;
maxn=maxn%2006; //防溢出
}
cout<<maxn<<endl; }
return 0;
}

被除数÷除数=商……余数。

由此可知,余数=被除数-商×除数 (*)

以下摘录自C++ Primer(P130)

操作符%称为“求余”或“求模”操作符,该操作符的操作数只能为整型。

如果两个操作数为正,结果也为正;如果两个操作数都为负数,结果也为负数;如果一个操作数为正数,一个操作数为负数,求模结果的符号取决于机器。

当操作数中有一个为负,一个为正是,求模操作结果值的符号可依据分子(被除数)或分母(除数)的符号而定。

如果求模的结果随分子的符号,则除出来的值向零一侧取整;如果求模与分母的符号匹配,则除出来的值向负无穷大一侧取整。

取模运算https://baike.baidu.com/item/%E5%8F%96%E6%A8%A1%E8%BF%90%E7%AE%97/10739384?fr=aladdin


title: woj1011 Finding Teamates 数学

date: 2020-03-12

categories: acm

tags: [acm,数学,woj]

一般题。数学(思维).

description

Big day is coming!

The Wuhan University Collegiate Programming Contest (WHUCPC 2006) will be held in this April. All the ACM lovers, excellent programmers and

computer geniuses in Wuhan University are of great joy, sweeping lots of problems at the Online Judges as preparation, waiting for this big day

to come.However, some students are still in trouble, because they have not found their teammates yet. So the WHUCPC Committee decided to

help these students. All the students that have no teammates are gathered in Peng Kun Square. The method to help them to find their

teammates is quite straightforward: all the students stand in a row, if some consecutive students form an increasing sequence

in their height, then these students must be put in the same team. That is, if there are 4 students who form an increasing sequence,

they must be put in one team and can't be divided long longo more than one team.

For example, if there are six students, standing as the following figure shows:

1 3 4 5 2 6

Where 1 represents the shortest student, and 6 represents the tallest student, etc.

Since (1, 3, 4, 5) forms an increasing sequence, they should put in the same team, and 2, 6 forms another increasing sequence,

so 2, 6 are put long longo another team. However, as we all know, the ACM teams are required 3 members each. In the above case, team 1

has 4 members, so the above alignment is invalid. The following figure shows some of the valid alignments:

1 3 4 2 5 6

But in some special cases, if the number of students cannot be divided evenly by 3, we ALLOW the LAST team has less than 3 members.

So if there are 5 students, the follow figure shows some of the valid and invalid alignments:

2 3 4 1 5 (ok) {234} {15}

1 5 2 3 4 (no) {15} {2 3 4 } team1 is not the last team

Now, given the number of the students, your mission is to tell us how many valid alignments there are.

Note, no two students are of the same height.

input

There are several test cases.

Each case contains a single long longeger n, representing the number of students who need to find teammates. (1<=n<=25)

output

Output an long longeger in a single line, which is the number of valid alignments. You are ensured that the result is less than 2^63.

Sample input

2

3

4

6

9

Sample output

1

1

3

19

1513

analyse

3人一队,n形如3x+y,y=012.

一看感觉是递推(直接从n想我是想不出来),先想了catalan,然后不对,估计就是自己推了。

假设知道了f(n),然后加了一个数y,那么至少有一种解就是y放在最后,ans+f(N)

然后y放到前面某个位置,就发现,其实相当于把n+1序列分成两个子序列(因为是递增无重复)f(x)f(n+1-x)。

这里每一组都可以看成1...x,因为无重复且递增,就有了递推

然后考虑y的位置,假设index从1开始,那必须是3x,否则不满足3人一队的条件。这样选择有n/3种。

然后这n/3种位置就有n/3种f(3x)f(n+1-3x)。

设x为数组下标,那么设y到了x组,这种方案,为f(3
x-1)(y固定在3x位置)f(n+1-3x)(后面的)*C(n,3x-1) (挑出放在前面的3x-1个数)

最后想特殊情况,没想到,就代入4试试

带入4算得4,这时候想到问题是,1234这种不符合要求(刚刚忘了排除了)

解决,直接n+1/3找合适位置

然后又有问题是当n+1%2==2时其实是可以放在最后的,特判就行

然后就这样写吧

最后是经验:想不出来,就试试递推

先想一般情况

再考虑有没有特殊情况

代入试一试

code

#include<iostream>
#include<cstdio> using namespace std; long long combine(int n,int m){
if(m>n-m) //一开始没有优化错了。一方面减循环,一方面减少乘法溢出
m=n-m;
long long x=1,y=1,z=2;
for(int i=n-m+1;i<=n;i++)
x*=i;
for(int i=1;i<=m;i++)
y*=i;
return x/y; } long long func(int n){
/*
if(n==0)
return 0; //一开始写成0了....这样返回后一乘就没了....
*/
if(n>=0&&n<4)
return 1;
int cnt=(n)/3;
long long ans=0; //注意ll
for(int i=1;i<=cnt;i++){
ans+=func(3*i-1)*func(n-3*i)*combine(n-1,3*i-1);
}
if(n%3==2)
ans+=func(n-1);
return ans;
} int main(){
int n;
while(cin>>n){
cout<<func(n)<<endl;
}
return 0;
}

woj1010 alternate sum 数学 woj1011 Finding Teamates 数学的更多相关文章

  1. CJOJ 1331 【HNOI2011】数学作业 / Luogu 3216 【HNOI2011】数学作业 / HYSBZ 2326 数学作业(递推,矩阵)

    CJOJ 1331 [HNOI2011]数学作业 / Luogu 3216 [HNOI2011]数学作业 / HYSBZ 2326 数学作业(递推,矩阵) Description 小 C 数学成绩优异 ...

  2. 装X数学:高雅的数学表示

    采用高雅的数学描述 转自于:研究生之路怎么走?       高雅的数学描述会提高你论文的等级和加强评审人对你基础功底的认可.例如泛函分析.集合.测度.度量空间和拓扑空间.现代代数.微分几何等数学方面的 ...

  3. PAT Advanced A1104 Sum of Number Segments (20) [数学问题]

    题目 Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For e ...

  4. zzulioj--1841--so easy!麻麻再也不用担心我的数学了!(数学水题)

    1841: so easy!麻麻再也不用担心我的数学了! Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 27  Solved: 15 SubmitSt ...

  5. 数学沉思录:古今数学思想的发展与演变 (Mario Livio 著)

    第1章 神秘的数学 (已看) 发现还是发明 第2章 神秘学:命理学家和哲学家 (已看) 毕达哥拉斯 进入柏拉图的洞穴 第3章 魔法师:大师和异端 (已看) 给我一个支点:我将撬起地球 阿基米德重写稿 ...

  6. Number of Containers(数学) 分类: 数学 2015-07-07 23:42 1人阅读 评论(0) 收藏

    Number of Containers Time Limit: 1 Second Memory Limit: 32768 KB For two integers m and k, k is said ...

  7. Codeforces 622F 「数学数论」「数学规律」

    题意: 给定n和k,求 1 ≤ n ≤ 109, 0 ≤ k ≤ 106 思路: 题目中给的提示是对于给定的k我们可以求出一个最高次为k+1的关于n的通项公式. 根据拉格郎日插值法,我们可以通过k+2 ...

  8. Codeforces 626D Jerry's Protest 「数学组合」「数学概率」

    题意: 一个袋子里装了n个球,每个球都有编号.甲乙二人从每次随机得从袋子里不放回的取出一个球,如果甲取出的球比乙取出的球编号大则甲胜,否则乙胜.保证球的编号xi各不相同.每轮比赛完了之后把取出的两球放 ...

  9. [原][c++][数学]osg常用图形数学算法小结

    1.cos趋近 // a reasonable approximation of cosine interpolation double smoothStepInterp( double t ) { ...

随机推荐

  1. window安装nvm

    先说一下背景,最近做的两个项目一个是祖传angularjs1.X版本另一个是react hooks结合tailwindcss,前者angularjs的node版本比较低,而tailwindcss的no ...

  2. 深度解读设备的“万能语言”HarmonyOS的分布式软总线能力

    摘要:本文分享鸿蒙分布式软总线,并对相关源代码进行解析,为在鸿蒙系统平台上工作的相关人员的信息参考和指导. 总线是一种内部结构,在计算机系统中,主机的各个部件通过总线相连,外部设备通过相应的接口电路再 ...

  3. ClickHouse入门:表引擎-HDFS

    前言插件及服务器版本服务器:ubuntu 16.04Hadoop:2.6ClickHouse:20.9.3.45 文章目录 简介 引擎配置 HDFS表引擎的两种使用形式 引用 简介 ClickHous ...

  4. uni-app开发经验分享四: 实现文字复制到选择器中

    这里分享一个我经常用到的一个方法,主要是用来复制文字内容,具体代码如下: var that=this; if(!document){ uni.setClipboardData({ data:复制的值, ...

  5. ORM动态表达式树查询

    前言 接口获取参数后,创建返回值模型的条件表达式作为参数,传入使用依赖注入实例化后的业务层. 业务层创建返回值模型的IQUERY后,再使用参数条件表达式.最后进行延迟查询. 代码实现 参数模型Demo ...

  6. 关掉IE提示“当前安全设置会使计算机有风险”

    我们先来看一下IE浏览器出现的提示窗口,该窗口位于最顶端,不点击设置的话,无法进行下一步的操作. 这时我们点击开始按钮 ,在弹出菜单中选择"运行"菜单项. 在打开的Windows运 ...

  7. Redis二进制安全

    为了便于理解,举一个例子: 在很多编辑器中,都会默认/n是换行字符,也就意味着一串字符存进去,涉及/n都会做一个默认的转义处理,这在编辑语言中,C也有这个特性,例如字符串Hello,\0 World! ...

  8. CF42A

    题意 给定两个序列 a 和 b. 序列 a 中的各个数之间的比例可以得出一个 x . 当 b 中比例满足 a 中比例,即 \(b_1\):\(b_2\):\(b_3\)-- \(=\) \(a_1\) ...

  9. loj10014数列分段二

    10014. 「一本通 1.2 练习 1」数列分段 II 题目描述 对于给定的一个长度为 n 的正整数数列 A ,现要将其分成 m 段,并要求每段连续,且每段和的最大值最小. 例如,将数列 4,2,4 ...

  10. mysql、sql server、oracle大比较

    MYSQL 多个数据库多个用户形式(最好每个数据库对应一个用户),占用内存小,适用于所有平台,开源免费 客户端和命令窗口,都是由数据库决定内容-> use datebase; 组函数在selec ...