日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[SinGuLaRiTy] 2017 百度之星程序设计大赛 初赛A

發布時間:2025/3/19 编程问答 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [SinGuLaRiTy] 2017 百度之星程序设计大赛 初赛A 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

【SinGuLaRiTy-1036】 Copyright (c) SinGuLaRiTy 2017. All Rights Reserved.

小C的倍數問題

Time Limit: 2000/1000 MS (Java/Others) ??Memory Limit: 32768/32768 K (Java/Others)

Problem Description

根據小學數學的知識,我們知道一個正整數x是3的倍數的條件是x每一位加起來的和是3的倍數。反之,如果一個數每一位加起來是3的倍數,則這個數肯定是3的倍數。

現在給定進制P,求有多少個B滿足P進制下,一個正整數是B的倍數的充分必要條件是每一位加起來的和是B的倍數。

Input

第一行一個正整數T表示數據組數(1<=T<=20)。

接下來T行,每行一個正整數P(2 < P < 1e9),表示一組詢問。

Output

對于每組數據輸出一行,每一行一個數表示答案。

Sample Input

1
10

Sample Output

3

Code

數學(這道題不算是"數論"吧......)

#include<cstring> #include<cmath> #include<algorithm> #include<iostream> #include<cstdio> #include<cstdlib>using namespace std;int T;int main() {cin>>T;for(int i=1;i<=T;i++){int n,cnt=0;scanf("%d",&n);n=n-1;for(int i=1;i*i<=n;i++){if(n%i==0)cnt+=2;if(i*i==n)cnt--;}printf("%d\n",cnt);}return 0; }

數據分割

Time Limit: 2000/1000 MS (Java/Others) ? ?Memory Limit: 32768/32768 K (Java/Others)

Problem Description

小w來到百度之星的賽場上,準備開始實現一個程序自動分析系統。
這個程序接受一些形如x_i = x_j 或x_?i??≠x_?j?? 的相等/不等約束條件作為輸入,判定是否可以通過給每個 w 賦適當的值,來滿足這些條件。
輸入包含多組數據。 然而粗心的小w不幸地把每組數據之間的分隔符刪掉了。 他只知道每組數據都是不可滿足的,且若把每組數據的最后一個約束條件去掉,則該組數據是可滿足的。
請幫助他恢復這些分隔符。

Input

第1行:一個數字L,表示后面輸入的總行數。
之后L行,每行包含三個整數,i,,j,e描述一個相等/不等的約束條件,若e=1,則該約束條件為x_i = x_j若e=0,則該約束條件為x?_i??!=x_?j?? 。
i,j,L≤100000
Lx?i??,x?j??≤L

Output

輸出共T+1行。
第一行一個整數T,表示數據組數。
接下來T行的第i行,一個整數,表示第i組數據中的約束條件個數。

Sample Input

6
2 2 1
2 2 1
1 1 1
3 1 1
1 3 1
1 3 0

Sample Output

1
6

Code

并查集

#include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<set>#define maxn 200011using namespace std;set<int> s[maxn];int T,n=200001; int cnt,vis[maxn],sta[maxn],top;struct Node {int fa[maxn];Node(){for(int i=1;i<=n;i++)fa[i]=i;}int find(int x){return x==fa[x]?x:(fa[x]=find(fa[x]));}void Union(int &x,int &y){x=find(x);y=find(y);if(x!=y)fa[x]=y;} }node;int x,y,id; int ans[maxn]; int lans=0,Case;void clear() {while (top){int now=sta[top--];node.fa[now]=now;s[now].clear();}ans[cnt++]=Case; }int main() {scanf("%d",&T);memset(vis,0,sizeof(vis));top=0;cnt=1;for (Case=1;Case<=T;Case++){scanf("%d%d%d",&x,&y,&id);if(vis[x]!=cnt)vis[x]=cnt,sta[++top]=x;if(vis[y]!=cnt)vis[y]=cnt,sta[++top]=y;x=node.find(x);y=node.find(y);if(s[x].size()>s[y].size()){int t=x;x=y;y=t;}if(id){if(s[x].find(y)!=s[x].end()){clear();continue;}node.Union(x,y);for(set<int>::iterator i=s[x].begin();i!=s[x].end();i++){int now=*i;now=node.find(now);s[now].erase(x);s[y].insert(now);s[now].insert(y);}s[x].clear();}else{if(x==y){clear();continue;}s[x].insert(y);s[y].insert(x);}}printf("%d\n",cnt-1);for(int i=1;i<cnt;i++)printf("%d\n",ans[i]-ans[i-1]);return 0; }

路徑交

Time Limit: 6000/3000 MS (Java/Others) ? ?Memory Limit: 132768/132768 K (Java/Others)

Problem Description

給定一棵n個點的樹,以及m條路徑,每次詢問第L條到第R條路徑的交集部分的長度(如果一條邊同時出現在2條路徑上,那么它屬于路徑的交集)。

Input

第一行一個數n(n<=500,000)接下來n-1行,每行三個數x,y,z,表示一條從x到y并且長度為z的邊第n+1行一個數m(m<=500,000)接下來m行,每行兩個數u,v,表示一條從u到v的路徑接下來一行一個數Q,表示詢問次數(Q<=500,000)接下來Q行,每行兩個數L和R

Output

Q行,每行一個數表示答案。

Sample Input

4
1 2 5
2 3 2
1 4 3
2
1 2
3 4
1
1 2

Sample Output

5

Code

線段樹區間合并+LCA

#include<cstdlib> #include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm>#define lson x<<1 #define rson x<<1|1using namespace std;const int maxn=500010; typedef long long ll;struct path {int a,b,c;path(){}path(int A,int B,int C){a=A,b=B,c=C;} }p[maxn],s[maxn<<2];int n,m,q,cnt; int mn[20][maxn<<1],Log[maxn<<1],pos[maxn],dep[maxn],fa[maxn]; int head[maxn],nxt[maxn<<1],val[maxn<<1],to[maxn<<1]; int cs[10];ll len[maxn];int MN(int a,int b) {return dep[a]<dep[b]?a:b; }int lca(int a,int b) {int x=pos[a],y=pos[b];if(x>y)swap(x,y);int k=Log[y-x+1];return MN(mn[k][x],mn[k][y-(1<<k)+1]); }bool cmp(int a,int b) {return dep[a]<dep[b]; }path mix(path x,path y) {if(!x.c||!y.c)return path(0,0,0);cs[1]=lca(x.a,y.a);cs[2]=lca(x.a,y.b);cs[3]=lca(x.b,y.a);cs[4]=lca(x.b,y.b);sort(cs+1,cs+5,cmp);int md=max(dep[x.c],dep[y.c]),nd=min(dep[x.c],dep[y.c]);if(dep[cs[1]]<nd||dep[cs[3]]<md)return path(0,0,0);elsereturn path(cs[3],cs[4],lca(cs[3],cs[4])); }inline int read() {int ret=0,f=1;char gc=getchar();while(gc<'0'||gc>'9'){if(gc=='-')f=-f; gc=getchar();}while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();return ret*f; }void add(int a,int b,int c) {to[cnt]=b;val[cnt]=c;nxt[cnt]=head[a];head[a]=cnt++; }void dfs(int x) {pos[x]=++pos[0];mn[0][pos[0]]=x;for(int i=head[x];i!=-1;i=nxt[i]){if(to[i]!=fa[x]){fa[to[i]]=x,dep[to[i]]=dep[x]+1,len[to[i]]=len[x]+val[i],dfs(to[i]);mn[0][++pos[0]]=x;}} }void build(int l,int r,int x) {if(l==r){s[x]=p[l];return ;}int mid=l+r>>1;build(l,mid,lson),build(mid+1,r,rson);s[x]=mix(s[lson],s[rson]); }path query(int l,int r,int x,int a,int b) {if(a<=l&&r<=b)return s[x];int mid=l+r>>1;if(b<=mid)return query(l,mid,lson,a,b);if(a>mid)return query(mid+1,r,rson,a,b);return mix(query(l,mid,lson,a,b),query(mid+1,r,rson,a,b)); }int main() {n=read();int i,j,a,b,c;memset(head,-1,sizeof(head));for(i=1;i<n;i++){a=read(),b=read(),c=read();add(a,b,c),add(b,a,c);}dep[1]=1;dfs(1);for(i=2;i<=2*n-1;i++)Log[i]=Log[i>>1]+1;for(j=1;(1<<j)<=2*n-1;j++)for(i=1;i+(1<<j)-1<=2*n-1;i++)mn[j][i]=MN(mn[j-1][i],mn[j-1][i+(1<<j-1)]);m=read();for(i=1;i<=m;i++){p[i].a=read(),p[i].b=read();p[i].c=lca(p[i].a,p[i].b);}build(1,m,1);q=read();for(i=1;i<=q;i++){a=read(),b=read();path ans=query(1,m,1,a,b);printf("%I64d\n",len[ans.a]+len[ans.b]-2*len[ans.c]);}return 0; }

迷宮出逃

Time Limit: 2000/1000 MS (Java/Others) ? Memory Limit: 32768/32768 K (Java/Others)

Problem Description

小明又一次陷入了大魔王的迷宮,在無人機的幫忙下,小明獲得了整個迷宮的草圖。
不同于一般的迷宮,魔王在迷宮里安置了機關,一旦觸碰,那么四個方向所在的格子,將翻轉其可達性(原先可通過的格子不可通過,反之亦然,機關可以反復觸發)。為了防止小明很容易地出逃,魔王在臨走前把鑰匙丟在了迷宮某處,只有拿到鑰匙,小明才能開門在出口處離開迷宮。
萬般無奈之下,小明想借助聰明的你,幫忙計算是否有機會離開這個迷宮,最少需要多少時間。(每一單位時間只能向四鄰方向走一步)

Input

第一行為 T,表示輸入數據組數。
下面 T 組數據,對于每組數據:
第一行是兩個數字 n, m(2 < n * m <= 64),表示迷宮的長與寬。
接下來 n 行,每行 m 個字符,‘.’表示空地可以通過,‘x’表示陷阱,‘*’表示機關,‘S’代表起點,‘E’代表出口,‘K’表示鑰匙(保證存在且只有一個)。

Output

對第 i 組數據,輸出
Case #i:
然后輸出一行,僅包含一個整數,表示最少多少步能夠拿到鑰匙并走出迷魂陣,如果不能則打出-1。

Sample Input

5 5 7 ...*x.. ...x... xEx.... *x...K. .x*...S 5 7 K..*x.. ...x... xEx.... *x..... .x*...S 5 7 ..K*x.. ..*x*.. xEx.... *x..... .x*...S 5 7 ..K*x.. .*xx*.. *E*.... xx..... .x*...S 4 4 S*.. **.. ...E ...K View Sample Input

Sample Output

Case #1:
11
Case #2:
13
Case #3:
13
Case #4:
11
Case #5:
-1

Code

狀態壓縮+BFS

#include<iostream> #include<cstdio> #include<queue> #include<cstring> #include<vector>typedef unsigned long long LL;using namespace std;int tx[]={-1,1,0,0}; int ty[]={0,0,-1,1}; int ii=0;struct datatype {int bs;LL zt1;LL zt2;int x;int y;datatype(){bs=0;zt1=0;x=0;y=0;zt2=0;} }; queue<datatype> team;int maps[70][70];struct mapdata {int x,y;LL zt1;int zt2;LL count(){LL ans=zt1%10000;ans=(ans+zt2)%10000;ans=(ans+x)%10000;ans=(ans+y)%10000;return ans;}bool operator<(const mapdata &b)const{if(this->x!=b.x)return this->x<b.x;if(this->y!=b.y)return this->y<b.y;if(this->zt1!=b.zt1)return this->zt1<b.zt1;return this->zt2<b.zt2;}bool operator==(const mapdata &b)const{if(this->x!=b.x)return this->x == b.x;if(this->y!=b.y)return this->y == b.y;if(this->zt1!=b.zt1)return this->zt1==b.zt1;return this->zt2==b.zt2;} };char s[70][100];vector<mapdata> has[10010];void deal() {memset(s,0,sizeof(s));memset(maps,0,sizeof(maps));int n,m;while(!team.empty())team.pop();datatype pt;cin>>n>>m;for(int i=0;i<=10000;i++)has[i].clear();for(int i=1;i<=n;i++)scanf("%s",s[i]+1);int qdx,qdy;for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){if(s[i][j]=='.') maps[i][j]=1;if(s[i][j]=='x') maps[i][j]=0;if(s[i][j]=='E') maps[i][j]=-666;if(s[i][j]=='*') maps[i][j]=-10;if(s[i][j]=='K') maps[i][j]=-66;if(s[i][j]=='S'){maps[i][j]=1,qdx=i,qdy=j;pt.bs=0,pt.zt1=0,pt.zt2=0,pt.x=qdx,pt.y=qdy;team.push(pt);}}}datatype pd;int ans=-1;while(!team.empty()){pt=team.front();team.pop();if(maps[pt.x][pt.y]==-666){if(pt.zt2==1){ans=pt.bs;break;}}for(int i=0;i<4;i++){pd.bs=pt.bs+1;pd.zt1=pt.zt1;pd.zt2=pt.zt2;pd.x=pt.x+tx[i];pd.y=pt.y+ty[i];if(pd.x<=0||pd.y<=0||pd.x>n||pd.y>m)continue;LL cj=(LL)(m)*(pd.x-1)+pd.y;LL zt=((pd.zt1>>(LL)(cj-1))&(LL)1);if(zt==0&&maps[pd.x][pd.y]==0)continue;if(zt==1){if(maps[pd.x][pd.y] != 0)continue;}LL ws1,ws2,ws3,ws4;ws1=0;ws2=0;ws3=0;ws4=0;ws1=(pd.x-2)*(m)+pd.y;ws2=(pd.x)*m+(pd.y);ws3=(pd.x-1)*m+(pd.y-1);ws4=(pd.x-1)*m+(pd.y+1);if(maps[pd.x][pd.y]==-10){if(pd.x-1>0){if((pd.zt1>>(ws1-(LL)1))&(LL)1)pd.zt1=pd.zt1-((LL)1<<(ws1-(LL)1));elsepd.zt1=pd.zt1+((LL)1<<(ws1-(LL)1));}if(pd.x+1<=n){if((pd.zt1>>(ws2-(LL)1))&(LL)1)pd.zt1=pd.zt1-((LL)1<<(ws2-(LL)1));elsepd.zt1=pd.zt1+((LL)1<<(ws2-(LL)1));}if(pd.y-1>0){if((pd.zt1>>(ws3-(LL)1))&(LL)1)pd.zt1=pd.zt1-((LL)1<<(ws3-(LL)1));elsepd.zt1=pd.zt1+((LL)1<<(ws3-(LL)1));}if(pd.y+1<=m){if((pd.zt1>>(ws4-(LL)1))&(LL)1)pd.zt1=pd.zt1-((LL)1<<(ws4-(LL)1));elsepd.zt1=pd.zt1+((LL)1<<(ws4-(LL)1));}}if(maps[pd.x][pd.y]==-66){pd.zt2=1;}mapdata h;h.x=pd.x;h.y=pd.y;h.zt1=pd.zt1;h.zt2=pd.zt2;int zzt=h.count();int vvv=0;for(int k=0;k<has[zzt].size();k++){if(has[zzt][k]==h){vvv=1;break;}}if(vvv)continue;has[zzt].push_back(h);team.push(pd);}}printf("Case #%d:\n",ii);cout<<ans<<endl; } int main() {int t;cin>>t;while(t--){ii++;deal();}return 0; }

今夕何夕

Time Limit: 2000/1000 MS (Java/Others) ? ?Memory Limit: 32768/32768 K (Java/Others)

Problem Description

今天是2017年8月6日,農歷閏六月十五。
小度獨自憑欄,望著一輪圓月,發出了“今夕何夕,見此良人”的寂寞感慨。
為了排遣郁結,它決定思考一個數學問題:接下來最近的哪一年里的同一個日子,和今天的星期數一樣?比如今天是8月6日,星期日。下一個也是星期日的8月6日發生在2023年。
小貼士:在公歷中,能被4整除但不能被100整除,或能被400整除的年份即為閏年。

Input

第一行為T,表示輸入數據組數。
每組數據包含一個日期,格式為YYYY-MM-DD。
1 ≤ T ≤ 10000
YYYY ≥ 2017
日期一定是個合法的日期

Output

對每組數據輸出答案年份,題目保證答案不會超過四位數。

Sample Input

3
2017-08-06
2017-08-07
2018-01-01

Sample Output

2023
2023
2024

Code

暴力枚舉

#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm>using namespace std;typedef long long ll;bool isrun(int year) {if (year%4==0&&year%100!=0||year%400==0)return true;return false; }int a[10000][13][32]; int month[]={29,31,28,31,30,31,30,31,31,30,31,30,31};int main() {memset(a,-1,sizeof(a));int p=0;for(int year=2017;year<=9999;year++){bool run=isrun(year);for(int m=1;m<=12;m++){if(run&&m==2){for(int d=1;d<=month[0];d++){a[year][m][d]=p;p++;p%=7;}continue;}for(int d=1;d<=month[m];d++){a[year][m][d]=p;p++;p%=7;}}}int t;scanf("%d",&t);while (t--){int y,m,d;scanf("%d-%d-%d",&y,&m,&d);for(int i=y+1;i<=9999;i++){if(a[i][m][d]==a[y][m][d]){printf("%d\n",i);break;}}}return 0; }

度度熊的01世界

Time Limit: 2000/1000 MS (Java/Others) ??Memory Limit: 32768/32768 K (Java/Others)

Problem Description

度度熊是一個喜歡計算機的孩子,在計算機的世界中,所有事物實際上都只由0和1組成。
現在給你一個n*m的圖像,你需要分辨他究竟是0,還是1,或者兩者均不是。
圖像0的定義:存在1字符且1字符只能是由一個連通塊組成,存在且僅存在一個由0字符組成的連通塊完全被1所包圍。
圖像1的定義:存在1字符且1字符只能是由一個連通塊組成,不存在任何0字符組成的連通塊被1所完全包圍。
連通的含義是,只要連續兩個方塊有公共邊,就看做是連通。
完全包圍的意思是,該連通塊不與邊界相接觸。

Input

本題包含若干組測試數據。 每組測試數據包含: 第一行兩個整數n,m表示圖像的長與寬。 接下來n行m列將會是只有01組成的字符畫。
滿足1<=n,m<=100

Output

如果這個圖是1的話,輸出1;如果是0的話,輸出0,都不是輸出-1。

Sample Input

32 32 00000000000000000000000000000000 00000000000111111110000000000000 00000000001111111111100000000000 00000000001111111111110000000000 00000000011111111111111000000000 00000000011111100011111000000000 00000000111110000001111000000000 00000000111110000001111100000000 00000000111110000000111110000000 00000001111110000000111110000000 00000001111110000000011111000000 00000001111110000000001111000000 00000001111110000000001111100000 00000001111100000000001111000000 00000001111000000000001111000000 00000001111000000000001111000000 00000001111000000000000111000000 00000000111100000000000111000000 00000000111100000000000111000000 00000000111100000000000111000000 00000001111000000000011110000000 00000001111000000000011110000000 00000000111000000000011110000000 00000000111110000011111110000000 00000000111110001111111100000000 00000000111111111111111000000000 00000000011111111111111000000000 00000000111111111111100000000000 00000000011111111111000000000000 00000000001111111000000000000000 00000000001111100000000000000000 00000000000000000000000000000000 32 32 00000000000000000000000000000000 00000000000000001111110000000000 00000000000000001111111000000000 00000000000000011111111000000000 00000000000000111111111000000000 00000000000000011111111000000000 00000000000000011111111000000000 00000000000000111111110000000000 00000000000000111111100000000000 00000000000001111111100000000000 00000000000001111111110000000000 00000000000001111111110000000000 00000000000001111111100000000000 00000000000011111110000000000000 00000000011111111110000000000000 00000001111111111111000000000000 00000011111111111111000000000000 00000011111111111111000000000000 00000011111111111110000000000000 00000000001111111111000000000000 00000000000000111111000000000000 00000000000001111111000000000000 00000000000111111110000000000000 00000000000011111111000000000000 00000000000011111111000000000000 00000000000011111111100000000000 00000000000011111111100000000000 00000000000000111111110000000000 00000000000000001111111111000000 00000000000000001111111111000000 00000000000000000111111111000000 00000000000000000000000000000000 3 3 101 101 011 View Sample Input

Sample Output

0
1
-1

Code

BFS判連通包含數量

#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<queue>#define ms(x,y) memset(x,y,sizeof(x))using namespace std;typedef long long ll; const int dx[]={0,1,0,-1},dy[]={1,0,-1,0};struct node {int x, y;node(int p,int q){x=p;y=q;} };int n,m; char a[110][110]; bool book[110][110];int bfs(int x,int y,char c) {int bao=1;queue<node> que;que.push(node(x,y));book[x][y]=1;while(que.size()){node p=que.front();que.pop();for (int i=0;i<4;i++){int px,py;px=p.x+dx[i];py=p.y+dy[i];if(px>=0&&px<n&&py>=0&&py<m){if(!book[px][py]&&a[px][py]==c){book[px][py]=1;que.push(node(px,py));}}elsebao=0;}}return bao; } int main() {while(~scanf("%d%d",&n,&m)){ms(book,0);ms(a,0);for(int i=0;i<n;i++){for(int j=0;j<m;j++){scanf(" %c",&a[i][j]);}}int num0=0,num1=0;int bao1=0,bao0=0;for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(!book[i][j]){if(a[i][j]=='0'){num0++;bao1+=bfs(i,j,'0');}else{num1++;bao0+=bfs(i,j,'1');}}}}if(num1==1&&bao1==0){printf("1\n");}else if(num1==1&&bao1==1){printf("0\n");}elseprintf("-1\n");}return 0; }

?

Time: 2017-08-12

轉載于:https://www.cnblogs.com/SinGuLaRiTy2001/p/7350254.html

總結

以上是生活随笔為你收集整理的[SinGuLaRiTy] 2017 百度之星程序设计大赛 初赛A的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。