Wannafly挑战赛22游记

幸运的人都是相似的,不幸的人各有各的不幸。

——题记

A-计数器

题目大意:

有一个计数器,计数器的初始值为\(0\),每次操作你可以把计数器的值加上\(a_1,a_2,\ldots,a_n\)中的任意一个整数,操作次数不限(可以为\(0\)次),问计数器的值对\(m\)取模后有几种可能。

思路:

由裴蜀定理易得,答案即为\(\frac m{\gcd(m,a_1,a_2,\ldots,a_n)}\)。

源代码:

#include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=101;
int a[N];
int main() {
const int n=getint(),m=getint();
int gcd=m;
for(register int i=1;i<=n;i++) {
a[i]=getint()%m;
gcd=std::__gcd(gcd,a[i]);
}
printf("%d\n",m/gcd);
return 0;
}

B-字符路径

题目大意:

给一个含\(n\)个点\(m\)条边的有向无环图(允许重边,点用\(1\)到\(n\)的整数表示),每条边上有一个字符,问图上有几条路径满足路径上经过的边上的字符组成的的字符串去掉空格后以大写字母开头,句号'.'结尾,中间都是小写字母,小写字母可以为\(0\)个。

思路:

拓扑序上DP,记录每个点可以对应多少个大写字母开头的字符串,若在前面加上空格有多少种方案,在后面加上空格有多少种方案(反图)。若当前边为'.'则计算对答案的贡献。

源代码:

#include<queue>
#include<cstdio>
#include<cctype>
#include<vector>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
inline int getch() {
register char ch=getchar();
while(!isalpha(ch)&&ch!='_'&&ch!='.') ch=getchar();
return ch;
}
const int N=5e4+1;
struct Edge {
int to;
char w;
};
std::vector<Edge> e[N],e2[N];
inline void add_edge(const int &u,const int &v,const char &w) {
e[u].push_back((Edge){v,w});
}
inline void add_edge2(const int &u,const int &v,const char &w) {
e2[u].push_back((Edge){v,w});
}
int n,m,ind[N],outd[N];
unsigned upper[N],space[N],space2[N],ans;
std::queue<int> q;
void kahn2() {
for(register int i=1;i<=n;i++) {
if(outd[i]==0) q.push(i);
}
while(!q.empty()) {
const int &x=q.front();
for(auto &j:e2[x]) {
const int &y=j.to;
const char &w=j.w;
if(w=='_') space2[y]+=space2[x]+1;
if(!--outd[y]) q.push(y);
}
q.pop();
}
}
void kahn() {
for(register int i=1;i<=n;i++) {
if(ind[i]==0) q.push(i);
}
while(!q.empty()) {
const int &x=q.front();
for(auto &j:e[x]) {
const int &y=j.to;
const char &w=j.w;
if(isupper(w)) upper[y]+=space[x]+1;
if(islower(w)) upper[y]+=upper[x];
if(w=='_') {
space[y]+=space[x]+1;
upper[y]+=upper[x];
}
if(w=='.') ans+=upper[x]*(space2[y]+1);
if(!--ind[y]) q.push(y);
}
q.pop();
}
}
int main() {
n=getint(),m=getint();
for(register int i=0;i<m;i++) {
const int u=getint(),v=getint();
const char w=getch();
add_edge(u,v,w);
add_edge2(v,u,w);
ind[v]++;
outd[u]++;
}
kahn2();
kahn();
printf("%u\n",ans);
return 0;
}

D-整数序列

题目大意:

给出一个长度为\(n\)的整数序列\(a_1,a_2,\ldots,a_n\),进行\(m\)次操作,操作分为两类:

  1. 给出\(l,r,v\),将\(a_{l\sim r}\)分别加上\(v\);
  2. 给出\(l,r\),询问\(\sum_{i=l}^r\sin(a_i)\)。

思路:

根据三角恒等变换:

\[\begin{align*}
\sin(\alpha+\beta)=\sin\alpha\cdot\cos\beta+\cos\alpha\cdot\sin\beta\\
\cos(\alpha+\beta)=\cos\alpha\cdot\cos\beta-\sin\alpha\cdot\sin\beta
\end{align*}
\]

用线段树维护区间\(\sum\sin(a_i)\)和区间\(\sum\cos(a_i)\)即可。

考虑写成复数的形式,\(\cos\)作为实部,\(\sin\)作为虚部。使用std::complex可以很方便的维护。

源代码:

#include<cmath>
#include<cstdio>
#include<cctype>
#include<complex>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
typedef long long int64;
typedef std::complex<double> comp;
const int N=2e5+1;
class SegmentTree {
#define _left <<1
#define _right <<1|1
#define mid ((b+e)>>1)
private:
comp val[N<<2],tag[N<<2];
void push_up(const int &p) {
val[p]=val[p _left]+val[p _right];
}
void push_down(const int &p) {
if(tag[p]==comp(1,0)) return;
val[p _left]*=tag[p];
val[p _right]*=tag[p];
tag[p _left]*=tag[p];
tag[p _right]*=tag[p];
tag[p]=comp(1,0);
}
public:
void build(const int &p,const int &b,const int &e) {
if(b==e) {
const int x=getint();
val[p]=comp(cos(x),sin(x));
return;
}
tag[p]=comp(1,0);
build(p _left,b,mid);
build(p _right,mid+1,e);
push_up(p);
}
void modify(const int &p,const int &b,const int &e,const int &l,const int &r,const comp &x) {
if(b==l&&e==r) {
tag[p]*=x;
val[p]*=x;
return;
}
push_down(p);
if(l<=mid) modify(p _left,b,mid,l,std::min(mid,r),x);
if(r>mid) modify(p _right,mid+1,e,std::max(mid+1,l),r,x);
push_up(p);
}
double query(const int &p,const int &b,const int &e,const int &l,const int &r) {
if(b==l&&e==r) return val[p].imag();
push_down(p);
double ret=0;
if(l<=mid) ret+=query(p _left,b,mid,l,std::min(mid,r));
if(r>mid) ret+=query(p _right,mid+1,e,std::max(mid+1,l),r);
return ret;
}
#undef _left
#undef _right
#undef mid
};
SegmentTree t;
int main() {
const int n=getint();
t.build(1,1,n);
const int m=getint();
for(register int i=0;i<m;i++) {
const int opt=getint(),l=getint(),r=getint();
if(opt==1) {
const int x=getint();
t.modify(1,1,n,l,r,comp(cos(x),sin(x)));
}
if(opt==2) {
printf("%.1f\n",t.query(1,1,n,l,r));
}
}
return 0;
}

Wannafly挑战赛22游记的更多相关文章

  1. Wannafly挑战赛25游记

    Wannafly挑战赛25游记 A - 因子 题目大意: 令\(x=n!(n\le10^{12})\),给定一大于\(1\)的正整数\(p(p\le10000)\)求一个\(k\)使得\(p^k|x\ ...

  2. Wannafly挑战赛24游记

    Wannafly挑战赛24游记 A - 石子游戏 题目大意: A和B两人玩游戏,总共有\(n(n\le10^4)\)堆石子,轮流进行一些操作,不能进行下去的人则输掉这局游戏.操作包含以下两种: 把石子 ...

  3. Wannafly挑战赛 22

    爆零祭 T1 这题第一反应gcd啊 所以就把每个a[i]对m取模 然后求它们的gcd 即res = gcd(a[1] % m, a[2] % m, ... , a[n] % m) ans = 1 + ...

  4. Wannafly 挑战赛22 D 整数序列 线段树 区间更新,区间查询

    题目链接:https://www.nowcoder.com/acm/contest/160/D 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言524288K ...

  5. 多项式 Wannafly挑战赛22

    后缀表达式 大整数(加法.乘法.gcd java) import java.math.BigInteger; import java.util.Scanner; class Work { String ...

  6. Wannafly挑战赛22 A-计数器(gcd,裴蜀定理)

    原题地址 题目描述 有一个计数器,计数器的初始值为0,每次操作你可以把计数器的值加上a1,a2,...,an中的任意一个整数,操作次数不限(可以为0次),问计数器的值对m取模后有几种可能. 输入描述: ...

  7. Wannafly挑战赛22

    B. 字符路径 给一个含n个点m条边的有向无环图(允许重边,点用1到n的整数表示),每条边上有一个字符,问图上有几条路径满足路径上经过的边上的字符组成的的字符串去掉空格后以大写字母开头,句号 '.' ...

  8. Wannafly挑战赛22 C 多项式(大数,多项式极限)

    链接:https://ac.nowcoder.com/acm/contest/160/C 来源:牛客网 多项式 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言 ...

  9. Wannafly挑战赛22 D 整数序列 (线段树维护三角函数值)

    链接:https://ac.nowcoder.com/acm/contest/160/D 来源:牛客网 整数序列 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语 ...

随机推荐

  1. LinuxUSB驱动程序调试--009:编写应用程序---验证协议【转】

    转自:http://biancheng.dnbcw.info/linux/257411.html [1] 如何编译X86下的 uBuntu APP---非常简单:            gcc -o ...

  2. [转载]如何在C++03中模拟C++11的右值引用std::move特性

    本文摘自: http://adamcavendish.is-programmer.com/posts/38190.htm 引言 众所周知,C++11 的新特性中有一个非常重要的特性,那就是 rvalu ...

  3. Centos 软连接和硬链接

    1.软链接: 建立软链接:ln -s /usr/local/node-v4.2.6-linux-x86/bin/node /usr/local/bin/node 解释:将/usr/local/node ...

  4. Python模块:Random(未完待续)

    本文基于Python 3.6.5的官文random编写. random模块简介 random为各种数学分布算法(distributions)实现了伪随机数生成器. 对于整数,是从一个范围中均匀选择(u ...

  5. 简单的UDP接受程序

    //功能:客服端发送UDP包,服务器接受到并打印出来//2015.9.13成功 #include <stdio.h>#include <sys/socket.h>#includ ...

  6. mysql数据库主从同步复制原理

    MySQL的Replication(英文为复制)是一个多MySQL数据库做主从同步的方案,特点是异步复制,广泛用在各种对MySQL有更高性能.更高可靠性要求的场合.与之对应的是另一个同步技术是MySQ ...

  7. TypeScript的配置文件 tsconfig.json

    //tsconfig.json指定了用来编译这个项目的根文件和编译选项 { "compilerOptions": { //compilerOptions:编译选项,可以被忽略,这时 ...

  8. Codeforces 948C Producing Snow(优先队列+思维)

    题目链接:http://codeforces.com/contest/948/problem/C 题目大意:给定长度n(n<=1e5),第一行v[i]表示表示第i堆雪的体积,第二行t[i]表示第 ...

  9. Shell学习笔记:<<EOF子命令

    在shell编程中,“EOF”通常与“<<”结合使用,“<<EOF”表示后续的输入作为子命令或子shell的输入,直到遇到“EOF”,再次返回到主调用shell,可将其理解为分 ...

  10. js中的异步[Important]

    js作为前端最主流的语言,主要处理页面显示变化(mutation)和异步(asynchronicity), js语言的基本要素和使用惯例的演化大都围绕着这两大主题,两者均值得总结和思考的主题, 这里先 ...