Add More Zero
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2245 Accepted Submission(s): 1053
Problem Description
There is a youngster known for amateur propositions concerning several mathematical hard problems.
Nowadays, he is preparing a thought-provoking problem on a specific type of supercomputer which has ability to support calculations of integers between 0
and (2
m
−1)
(inclusive).
As a young man born with ten fingers, he loves the powers of 10
so much, which results in his eccentricity that he always ranges integers he would like to use from 1
to 10
k
(inclusive).
For the sake of processing, all integers he would use possibly in this interesting problem ought to be as computable as this supercomputer could.
Given the positive integer m
, your task is to determine maximum possible integer k
that is suitable for the specific supercomputer.
Input
The input contains multiple test cases. Each test case in one line contains only one positive integer m
, satisfying 1≤m≤ 105 .
Output
For each test case, output "Case #x
: y
" in one line (without quotes), where x
indicates the case number starting from 1
and y
denotes the answer of corresponding case.
Sample Input
Sample Output
Case #1: 0 Case #2: 19
该题求 2m -1 在10进制下的位数-1,我们注意到 k=log102m-1 ,因为 2m-1 != 10k1 且 2m!=10k2($ k_1 k_2 $都为任意正整数),所以 k=log102m-1 ,把m提出来,于是k=mlog102,这样O(1)即可算出答案。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<bits/stdc++.h> 2 #define clr(x) memset(x,0,sizeof(x)) 3 using namespace std; 4 int main() 5 { 6 int m; 7 int t = 1; 8 while(cin >> m) { 9 10 cout << "Case #" << t ++ << ": " << (int)(m*0.30102999566)<< " "; 11 } 12 return 0; 13 }
Balala Power!
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 4809 Accepted Submission(s): 387
Problem Description
![](http://acm.hdu.edu.cn/data/images/C642-1002-1.jpg)
Talented Mr.Tang has n
strings consisting of only lower case characters. He wants to charge them with Balala Power (he could change each character ranged from a to z into each number ranged from 0 to 25, but each two different characters should not be changed into the same number) so that he could calculate the sum of these strings as integers in base 26
hilariously.
Mr.Tang wants you to maximize the summation. Notice that no string in this problem could have leading zeros except for string "0". It is guaranteed that at least one character does not appear at the beginning of any string.
The summation may be quite large, so you should output it in modulo 109
+7
.
Input
The input contains multiple test cases.
For each test case, the first line contains one positive integers n
, the number of strings. (1≤n≤100000)
Each of the next n
lines contains a string s
i
consisting of only lower case letters. (1≤|s
i
|≤100000,∑|s
i
|≤10
6
)
Output
For each test case, output "Case #x
: y
" in one line (without quotes), where x
indicates the case number starting from 1
and y
denotes the answer of corresponding case.
Sample Input
Sample Output
Case #1: 25 Case #2: 1323 Case #3: 18221
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 2 #define clr(x) memset(x,0,sizeof(x)) 3 #define mod 1000000007 4 #define LL long long 5 using namespace std; 6 char ss[100020]; 7 int n,m,k,l,t,minc,len; 8 int charc[50][100020]; 9 bool firsted[200]; 10 int inf[200]; 11 bool comp(int a,int b) 12 { 13 for(int i=m;i>=0;i--) 14 if(charc[a-'a'][i]!=charc[b-'a'][i]) 15 return charc[a-'a'][i]>charc[b-'a'][i]; 16 return 0; 17 } 18 int max(int a,int b) 19 { 20 return a>b?a:b; 21 } 22 int main() 23 { 24 int T,kase=0; 25 LL ans,mul; 26 while(scanf("%d",&n)!=EOF) 27 { 28 m=0; 29 clr(firsted); 30 clr(charc); 31 printf("Case #%d: ",++kase); 32 for(int i=1;i<=n;i++) 33 { 34 scanf("%s",ss); 35 len=strlen(ss); 36 if(len>0) firsted[ss[0]]=1; 37 reverse(ss,ss+len); 38 for(int j=0;j<len;j++) 39 charc[ss[j]-'a'][j]++; 40 m=max(m,len); 41 } 42 for(int i='a';i<='z';i++) 43 { 44 // cout<<i<<":"; 45 for(int j=0;j<m;j++) 46 { 47 if(charc[i-'a'][j]>25) 48 { 49 charc[i-'a'][j+1]+=charc[i-'a'][j]/26; 50 charc[i-'a'][j]%=26; 51 } 52 // cout<<" "<<charc[i-'a'][j]; 53 } 54 while(charc[i-'a'][m]>25) 55 { 56 charc[i-'a'][m+1]+=charc[i-'a'][m]/26; 57 charc[i-'a'][m]%=26; 58 m++; 59 // cout<<" "<<charc[i-'a'][m]; 60 } 61 if(charc[i-'a'][m]>0) 62 m++; 63 // cout<<endl; 64 } 65 clr(inf); 66 ans=0; 67 for(int i=0;i<=25;i++) 68 { 69 for(int j='a';j<='z';j++) 70 if((i==0 && firsted[j]==0) || (i!=0 && inf[j-'a']==0)) 71 { 72 minc=j; 73 break; 74 } 75 for(int j='a';j<='z';j++) 76 if((i==0 && firsted[j]==0) || (i!=0 && inf[j-'a']==0)) 77 if(!comp(j,minc)) 78 minc=j; 79 mul=1; 80 for(int j=0;j<=m;j++) 81 { 82 ans=(ans+(LL)charc[minc-'a'][j]*mul*(LL)i)%mod; 83 mul=(mul*26)%mod; 84 } 85 inf[minc-'a']=1; 86 // cout<<ans<<" "<<maxc<<endl; 87 } 88 printf("%lld ",ans); 89 } 90 return 0; 91 }
Colorful Tree
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 392 Accepted Submission(s): 68
Problem Description
There is a tree with n
nodes, each of which has a type of color represented by an integer, where the color of node i
is c
i
.
The path between each two different nodes is unique, of which we define the value as the number of different colors appearing in it.
Calculate the sum of values of all paths on the tree that has n(n−1)2
paths in total.
Input
The input contains multiple test cases.
For each test case, the first line contains one positive integers n
, indicating the number of node. (2≤n≤200000)
Next line contains n
integers where the i
-th integer represents c
i
, the color of node i
. (1≤c
i
≤n)
Each of the next n−1
lines contains two positive integers x,y
(1≤x,y≤n,x≠y)
, meaning an edge between node x
and node y
.
It is guaranteed that these edges form a tree.
Output
For each test case, output "Case #x
: y
" in one line (without quotes), where x
indicates the case number starting from 1
and y
denotes the answer of corresponding case.
Sample Input
Sample Output
Case #1: 6 Case #2: 29
该题应该按照颜色来求解,而不是点和路径来求解。每种颜色对答案的贡献为所有经过该颜色的点的路径数,也就是所有的路径数减去没有经过该颜色的点的路径数。那么求解量转化成该颜色的点的路径数之后,对于每个颜色我们接下来则是找树上没有经过该颜色的块,统计块内的点的数量,这些点都是互相可达且不经过该颜色,那么块内不经过该颜色的路径数就是块中点数ki*(ki-1)/2。那么很浅显的一个思路就是对于每个颜色都用dfs求解出不经过颜色的块然后用总的路径数减去每个块内的路径数,即为每个颜色的贡献。可是这个时间复杂度会很高,那能不能在一次dfs内解决呢?我们可以发觉在对每个颜色的点的dfs中,我们只有dfs到该颜色的点才有操作,去计算块内点的数量。那么一遍dfs的话,对于每个点它对应的颜色都应该有相应的操作,这样思路就显而易见了。我们在一次dfs中可以统计对于每个点子树中节点的数量,然后我们对于每个颜色都设置一个栈来存储该颜色的点的编号。当到dfs到某个点时,我们将该点的编号入相应颜色的栈继续向下dfs。然后回到该点的时候将该颜色在该点之后入栈的点全部都pop出来,然后该颜色的块的点的数量即为该点子树的点的数量减去pop出来的点的子树的数量,计算下路径数加入答案中作为没有经过该颜色的点的路径数的贡献。最后拿每个颜色的所有路径数的和,减去这个没有经过每个颜色的点的路径数的总和即为答案。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<stack> 5 #include<vector> 6 #define clr(x) memset(x,0,sizeof(x)) 7 #define clr_1(x) memset(x,-1,sizeof(x)) 8 #define LL long long 9 using namespace std; 10 int head[200005],cnt,ncnt,dif,col[200005]; 11 LL ans; 12 int n,m,k,t,s,l; 13 struct edg 14 { 15 int next,to; 16 }edge[400005]; 17 void addedge(int u,int v) 18 { 19 edge[++cnt].next=head[u]; 20 edge[cnt].to=v; 21 head[u]=cnt; 22 return ; 23 } 24 int some[200005]; 25 stack<int> sta[200005]; 26 void dfs(int u,int pred) 27 { 28 some[u]=1; 29 sta[col[u]].push(u); 30 for(int i=head[u];i!=-1;i=edge[i].next) 31 { 32 if(edge[i].to!=pred) 33 { 34 dfs(edge[i].to,u); 35 some[u]+=some[edge[i].to]; 36 dif=some[edge[i].to]; 37 while(sta[col[u]].top()!=u) 38 { 39 dif-=some[sta[col[u]].top()]; 40 sta[col[u]].pop(); 41 } 42 ans+=(LL)dif*(dif-1)/2; 43 } 44 } 45 return ; 46 } 47 int main() 48 { 49 int kase=0,u,v,diff,p; 50 while(scanf("%d",&n)!=EOF) 51 { 52 for(int i=1;i<=n;i++) 53 while(!sta[i].empty()) 54 sta[i].pop(); 55 printf("Case #%d: ",++kase); 56 clr_1(head); 57 cnt=ncnt=0; 58 for(int i=1;i<=n;i++) 59 scanf("%d",&col[i]); 60 for(int i=1;i<n;i++) 61 { 62 scanf("%d%d",&u,&v); 63 addedge(u,v); 64 addedge(v,u); 65 } 66 ans=0; 67 clr(some); 68 dfs(1,1); 69 for(int i=1;i<=n;i++) 70 { 71 diff=n; 72 while(!sta[i].empty()) 73 { 74 p=sta[i].top(); 75 diff-=some[p]; 76 sta[i].pop(); 77 } 78 ans+=(LL)diff*(LL)(diff-1)/2; 79 } 80 ans=(LL)n*n*(n-1)/2-ans; 81 printf("%lld ",ans); 82 } 83 return 0; 84 }
KazaQ's Socks
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2375 Accepted Submission(s): 1074
At the beginning, he has n
Every morning, he puts on a pair of socks which has the smallest number in the closets.
Every evening, he puts this pair of socks in the basket. If there are n−1
KazaQ would like to know which pair of socks he should wear on the k
For each case, there is a line contains two numbers n,k
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<bits/stdc++.h> 2 #define clr(x) memset(x,0,sizeof(x)) 3 #define LL long long 4 using namespace std; 5 int main() 6 { 7 LL n,k,kase=0,t,l,ans; 8 while(scanf("%lld%lld",&n,&k)!=EOF) 9 { 10 printf("Case #%lld: ",++kase); 11 if(k<=n) 12 printf("%lld ",k); 13 else 14 { 15 k-=n; 16 t=k/(n-1); 17 l=k%(n-1); 18 if(l!=0) 19 printf("%lld ",l); 20 else if(t&1) 21 printf("%lld ",n-1); 22 else 23 printf("%lld ",n); 24 } 25 } 26 return 0; 27 }
Function
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1464 Accepted Submission(s): 688
Define that the domain of function f
Please calculate the quantity of different functions f
Two functions are different if and only if there exists at least one integer from 0
The answer may be too large, so please output it in modulo 10
For each case:
The first line contains two numbers n,
The second line contains n
The third line contains m
It is guaranteed that ∑n≤10
这题列下题目中给的式子将$ a_i $代入就能发现,这些式子构成了一些不相交且包含 1~n所有数的循环。于是我们对于a 中的每个循环,查找b中循环节为其循环节因数的循环的数量乘上该循环长度,将其求和,即为a中该循环可对应的f的映射数。那么a中每个循环的f的映射数相乘,即为对应的不同的映射数。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 2 #define clr(x) memset(x,0,sizeof(x)) 3 #define mod 1000000007 4 #define LL long long 5 using namespace std; 6 int a[100010]; 7 int b[100010]; 8 int knot[100010],inf[100010]; 9 /*int prime[100010],infp[100010]; 10 int ncnt; 11 void primer(int n) 12 { 13 ncnt=0; 14 clr(infp); 15 infp[1]=infp[2]=1; 16 for(int i=2;i<=n;i++) 17 { 18 if(!inf[i]) 19 { 20 prime[++ncnt]=i; 21 } 22 for(int j=1;j<=ncnt;j++) 23 { 24 if(i*prime[j]>n) break; 25 infp[i*prime[j]]=1; 26 if(i%prime[j]==0) 27 break; 28 } 29 } 30 return ; 31 }*/ 32 LL deal(LL l) 33 { 34 LL ans=0; 35 for(int i=1;i<=sqrt(l);i++) 36 { 37 if(l%i==0) 38 { 39 ans=(ans+(LL)knot[l/i]*(LL)(l/i))%mod; 40 if(l/i!=i) ans=(ans+(LL)knot[i]*(LL)i)%mod; 41 } 42 } 43 return ans; 44 } 45 int main() 46 { 47 int kase=0; 48 int n,m,sized,minsize,k,l; 49 LL t,ans; 50 // init(); 51 while(scanf("%d%d",&n,&m)!=EOF) 52 { 53 printf("Case #%d: ",++kase); 54 for(int i=0;i<n;i++) 55 scanf("%d",&a[i]); 56 for(int i=0;i<m;i++) 57 scanf("%d",&b[i]); 58 clr(inf); 59 clr(knot); 60 sized=m; 61 minsize=0; 62 t=0; 63 while(sized) 64 { 65 if(inf[minsize]!=0) 66 minsize++; 67 l=0; 68 k=minsize; 69 while(inf[k]==0) 70 { 71 inf[k]=1; 72 sized--; 73 k=b[k]; 74 l++; 75 // cout<<k<<endl; 76 } 77 knot[l]++; 78 // cout<<endl; 79 } 80 ans=1; 81 clr(inf); 82 sized=n; 83 minsize=0; 84 while(sized) 85 { 86 while(inf[minsize]!=0) 87 minsize++; 88 k=minsize; 89 l=0; 90 while(inf[k]==0) 91 { 92 inf[k]=1; 93 sized--; 94 k=a[k]; 95 l++; 96 } 97 ans=(ans*deal((LL)l))%mod; 98 } 99 printf("%lld ",ans%mod); 100 } 101 return 0; 102 }