HZNU Training 1 for Zhejiang Provincial Collegiate Programming Contest
赛后总结:
TJ:今天我先到实验室,开始看题,一眼就看了一道防AK的题目,还居然觉得自己能做wwww。然后金姐和彭彭来了以后,我和他们讲了点题目。然后金姐开始搞dfs,我和彭彭看榜研究F题。想了很久脑子都炸了,做不下去了。然后在等金姐做完以后,彭彭说了D题题意,然后金姐开始写另外一道搜索题。做完两道搜索以后,金姐让我写那道线段树的题目。我搞了很久。。。然后超时了,因为要用素数筛,然后加了素数筛也做不出呜呜。金姐和彭彭把Ant的题目公式给搞出来了。开始敲。后来是因为取模的问题,就一直没搞成功。比赛吃完饭回来搞了一下,就。。ac了。
呜呜呜,我要更努力,不给队友拖后腿。
题解:
农民开始播种。在n*m的田地上有几块石头,一开始机器从左上角点开始出发,问是否开机器,经过田地上的每个点只用一次,大石头不能到达。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<set> #include<map> #include<string> #include<vector> #include<ctime> #include<stack> using namespace std; const int MAXN = 2e5+50; #define MAX_DISTANCE 0x3f3f3f3f #define mm(a,b) memset(a,b,sizeof(a)) #define ll long long #define SIGN(A) ((A > 0) ? 1 : -1) #define NO_DISTANCE 1000000 int n, m, sum; char a[100][100]; int vis[100][100]; int dir[4][2] = { 1,0,-1,0,0,1,0,-1 }; bool check(int x, int y) { if (x < 0 || x >= n || y < 0 || y >= m || vis[x][y] || a[x][y] == 'S') return false; return true; } bool dfs(int x, int y, int k) { if (k <= 1) return true; vis[x][y] = 1; for (int i = 0; i < 4; ++i) { int xx = x + dir[i][0]; int yy = y + dir[i][1]; if (check(xx, yy)) { if (dfs(xx, yy, k - 1)) return true; vis[xx][yy] = 0; } } return false; } int main() { while (scanf("%d %d", &n, &m), n || m) { mm(vis, 0); for (int i = 0; i < n; ++i) scanf("%s", a[i]); sum = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (a[i][j] == '.') sum++; } } if (dfs(0, 0, sum)) printf("YES\n"); else printf("NO\n"); } //system("pause"); return 0; }
天使的朋友营救天使,经过一个点用花费1时间,打败guard需要花费1(可打败所有guard),问最后营救天使最少需要花费多少时间。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<set> #include<map> #include<string> #include<vector> #include<ctime> #include<stack> using namespace std; const int MAXN = 2e5 + 50; #define MAX_DISTANCE 0x3f3f3f3f #define mm(a,b) memset(a,b,sizeof(a)) #define ll long long #define SIGN(A) ((A > 0) ? 1 : -1) #define NO_DISTANCE 1000000 int n, m, sum; char a[300][300]; int vis[300][300]; int dir[4][2] = { 1,0,-1,0,0,1,0,-1 }; bool check(int x, int y) { if (x < 0 || x >= n || y < 0 || y >= m || vis[x][y] || a[x][y] == '#') return false; return true; } struct node { int x, y, t; bool friend operator <(node a, node b) { return a.t > b.t; } }; priority_queue<node>q; int bfs() { while (!q.empty()) { node now = q.top(); q.pop(); //cout << now.x << " " << now.y <<" "<<now.t<< endl; if (a[now.x][now.y] == 'a') return now.t; for (int i = 0; i < 4; ++i) { int xx = now.x + dir[i][0]; int yy = now.y + dir[i][1]; if (check(xx, yy)) { vis[xx][yy] = 1; if (a[xx][yy] == 'x') q.push(node{ xx,yy,now.t + 2 }); else q.push(node{ xx,yy,now.t + 1 }); } } } return -1; } int main() { while (~scanf("%d %d", &n, &m)) { mm(vis, 0); while (!q.empty()) q.pop(); for (int i = 0; i < n; ++i) scanf("%s", a[i]); sum = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (a[i][j] == 'r') { q.push(node{ i,j,0 }); vis[i][j] = 1; } } } int time = bfs(); if (time == -1) printf("Poor ANGEL has to stay in the prison all his life.\n"); else printf("%d\n", time); } //system("pause"); return 0; }
G - Prime Query ZOJ - 3911
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #define mid(a,b) ((a+b)>>1) #define LL int #define maxn 110000 #define IN freopen("in.txt","r",stdin); using namespace std; char is_prime[maxn*100]; void sieve() { int m=(int)sqrt((maxn*100)+0.5); fill(is_prime,is_prime+(maxn*100),1); is_prime[0]=is_prime[1]=0; for(int i=2;i<=m;i++) if(is_prime[i]) for(int j=i*i;j<(maxn*100);j+=i) is_prime[j]=0; } int n,q; LL num[maxn]; struct Tree { int left,right; LL sum; LL val,lazy; }tree[maxn<<2]; /*递归建树*/ void build(int i,int left,int right) { tree[i].left=left; tree[i].right=right; tree[i].lazy=0; if(left==right){ tree[i].val=num[left]; tree[i].sum=(is_prime[num[left]]? 1:0); return ; } int mid=mid(left,right); build(i<<1,left,mid); build(i<<1|1,mid+1,right); tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum; } /*区间修改,标记下传:每当访问到当前结点的子节点时,下传标记*/ void pushdown(int i) { if(tree[i].lazy){ int tmp = (is_prime[tree[i].lazy]? 1:0); tree[i<<1].val=tree[i].lazy; tree[i<<1|1].val=tree[i].lazy; tree[i<<1].lazy=tree[i].lazy; tree[i<<1|1].lazy=tree[i].lazy; tree[i<<1].sum=(tree[i<<1].right-tree[i<<1].left+1)*tmp; tree[i<<1|1].sum=(tree[i<<1|1].right-tree[i<<1|1].left+1)*tmp; tree[i].lazy=0; /*下传后清零*/ } } /*区间修改,d为改变量*/ void update(int i,int left,int right,LL d) { if(tree[i].left==left&&tree[i].right==right) { int tmp = (is_prime[d]? 1:0); tree[i].sum=(right-left+1)*tmp; tree[i].val=d; tree[i].lazy=d; return ; } pushdown(i); int mid=mid(tree[i].left,tree[i].right); if(right<=mid) update(i<<1,left,right,d); else if(left>mid) update(i<<1|1,left,right,d); else { update(i<<1,left,mid,d); update(i<<1|1,mid+1,right,d); } tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum; } /*单点修改,d为改变量*/ void update(int i,int x,LL d) { if(tree[i].left==tree[i].right){ tree[i].val+=d; tree[i].sum=(is_prime[tree[i].val]? 1:0); return; } pushdown(i); int mid=mid(tree[i].left,tree[i].right); if(x<=mid) update(i<<1,x,d); else update(i<<1|1,x,d); tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum; } /*区间结果查询*/ LL query(int i,int left,int right) { if(tree[i].left==left&&tree[i].right==right) return tree[i].sum; pushdown(i); int mid=mid(tree[i].left,tree[i].right); if(right<=mid) return query(i<<1,left,right); else if(left>mid) return query(i<<1|1,left,right); else return query(i<<1,left,mid)+query(i<<1|1,mid+1,right); } int main(int argc, char const *argv[]) { //IN; sieve(); int t;scanf("%d",&t); while(t--) { scanf("%d %d",&n,&q); for(int i=1;i<=n;i++) scanf("%d",&num[i]); build(1,1,n); while(q--) { char c; while(c=getchar()){ if(c=='A'||c=='R'||c=='Q') break; } if(c=='A'){ int v,l; scanf("%d %d",&v,&l); update(1,l,v); } if(c=='R'){ int a,l,r; scanf("%d %d %d",&a,&l,&r); update(1,l,r,a); } if(c=='Q'){ int l,r; scanf("%d %d",&l,&r); printf("%d\n", query(1,l,r)); } } } return 0; }
小蚂蚁从立方体角上的一点爬到立方体对面的点,给你一条最长的一边n长度,另外两条边1<=a,b<=n,问所有最短长度L^2的合(答案对1e9+7取模)。
公式:
L^2=(n*n)*(n*(n+1))/2+(n*n)*(n+1)*(n+1)/2+n*(n+1)*(2*n+1)*(n+2)/6;
(具体看代码)
因为n的范围在1~10^14,所以计算取模的时候一定要小心。还有在/6的时候需要进行特判。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<set> #include<map> #include<string> #include<vector> #include<ctime> #include<stack> using namespace std; const int maxn = 2e5 + 50; #define MAX_DISTANCE 0x3f3f3f3f #define mm(a,b) memset(a,b,sizeof(a)) #define ll long long #define SIGN(A) ((A > 0) ? 1 : -1) #define NO_DISTANCE 1000000 const int INF = 0x3f3f3f3f; #define LL long long int #define mod 1000000007 int main() { int t; ll n; cin >> t; while (t--) { scanf("%lld", &n); n %= mod; ll ans = (n*(n + 1) / 2) % mod*((n * n) % mod); ans = (ans + (((n*(n + 1) / 2) % mod)*((n*(n + 1) / 2) % mod)) % mod) % mod; if (n % 3 == 0||(n+1)%3==0) { ans = (ans + (((n*(n + 1) / 6) % mod)*(((2 * n + 1)*(n + 2)) % mod))) % mod; } else { ans = (ans + (((n*(n + 1) / 2) % mod)*(((2 * n + 1)*(n + 2)/3) % mod))) % mod; } //ans = (ans + (((n*(n + 1) / 2) % mod)*(((2 * n + 1)*(n + 2)) % mod) / 3)) % mod; // // ll ans = ((((n * n) % mod )*n % mod) *(n + 1)) % mod / 2; //ans = (ans + ((((n * n % mod) *(n + 1) % mod)*(n + 1) % mod)%mod )/ 4) % mod; //ans = (ans + (((n * (n + 1) % mod)*((2 * n + 1) % mod ) % mod)%mod*(n + 2) % mod) / 6) % mod; cout << ans << endl; } //system("pause"); return 0; }