1160 (20 Forever dfs+回溯剪枝)
题目描述:
“Forever number” is a positive integer A with K digits, satisfying the following constrains:
- the sum of all the digits of A is m;
- the sum of all the digits of A+1 is n; and
- the greatest common divisor of m and n is a prime number which is greater than 2.
Now you are supposed to find these forever numbers.
Input Specification:
Each input file contains one test case. For each test case, the first line contains a positive integer N (≤5). Then N lines follow, each gives a pair of K (3<K<10) and m (1<m<90), of which the meanings are given in the problem description.
Output Specification:
For each pair of K and m, first print in a line Case X
, where X
is the case index (starts from 1). Then print n and A in the following line. The numbers must be separated by a space. If the solution is not unique, output in the ascending order of n. If still not unique, output in the ascending order of A. If there is no solution, output No Solution
.
Sample Input:
2
6 45
7 80
Sample Output:
Case 1
10 189999
10 279999
10 369999
10 459999
10 549999
10 639999
10 729999
10 819999
10 909999
Case 2
No Solution
方法一:dfs
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
struct node {
int n, A; //n A+1各个位数之和 A求的数
};
int n,m,N,k;
vector<node> ans;
bool cmp(node a,node b) { //按照n递增排序,n相同按照A递增排序
if(a.n != b.n) return a.n < b.n;
else return a.A < b.A;
}
bool isPrime(int x) { //判断是不是大于3的素数
if(x <= 2) return false;
for(int i = 2; i * i<= x; i++)
if(x % i == 0) return false;
return true;
}
int gcd(int a,int b) { //求最大公约数
return b == 0 ? a : gcd(b,a%b);
}
int digit_sum(int A) { //各个位数之和
int sum = 0;
while(A) {
sum += A%10;
A/=10;
}
return sum;
}
void DFS(int A, int sum, int rest_K) { //A目标数 sum当前位数和 rest_k剩余k位
if(rest_K == 0) { //递归边界
if(sum == m) {
int n = digit_sum(A + 1);
if(isPrime(gcd(m, n)))
ans.push_back({n, A});
}
} else if(rest_K > 0) {
for(int i = 0; i <= 9; i++) {
//如果当前位为i,即时剩余的rest_k - 1位全部取9也无法达到m,则剪枝
//当前位数和sum,当前位取i,已经超过了m也剪枝
if(sum + i + (rest_K-1)*9 >= m && sum + i <= m)
DFS(A*10 + i, sum+i, rest_K-1);
}
}
}
int main() {
scanf("%d",&N); //样例数
for(int x = 1; x <= N; x++) {
ans.clear();
printf("Case %d\n",x);
scanf("%d%d",&k,&m); //k A的位数, m A各位数之和
for(int i = 1; i <= 9; i++) //i是最高位的数的取值
DFS(i, i, k-1);
if(ans.empty()) {
printf("No Solution\n");
continue;
}
sort(ans.begin(), ans.end(), cmp);
for(int i = 0; i < ans.size(); i++) {
printf("%d %d\n",ans[i].n, ans[i].A);
}
}
return 0;
}
方法二:数学规律
按照尾数为99的数枚举,加一不进位的话,这两个数的各位之和相差的是1,无论如何也不会有不小于三的最小公约数
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
using namespace std;
int k, n,m,a,b,c,d,num;
bool f=false;
struct node {
int n,a;
};
int gcd(int a,int b) {
return b==0?a:gcd(b,a%b);
}
bool isPrime(int n) {
if(n<=2)return false;
int sq=(int)sqrt(n);
for(int j=2; j<=sq; j++) {
if(n%j==0)return false;
}
return true;
}
int sum(int n) {
return n==0? 0: n%10 + sum(n/10);
}
bool cmp(node &a,node &b) {
if(a.n!=b.n)return a.n<b.n;
return a.a<b.a;
}
int main() {
scanf("%d", &n);
for(int i=0; i<n; i++) {
scanf("%d %d",&k,&m);
printf("Case %d\n",i+1);
a=(int)pow(10,k-3);//-3
b=a*10;
vector<node> v;
for(int x=a; x<b; x++) {
c=x*100+99;
d=sum(c+1);
if(sum(c)==m && isPrime(gcd(d,m))) {
v.push_back({d,c});
}
}
if(v.size()==0) printf("No Solution\n");
else {
sort(v.begin(),v.end(),cmp);
for(auto it:v) {
printf("%d %d\n",it.n,it.a);
}
}
}
return 0;
}
1161(25 合并链表)
Merging Linked Lists (25分)
题目描述:
Merging Linked Lists (25分)
Given two singly linked lists L1=a1→a2→⋯→a**n−1→a**n and L2=b1→b2→⋯→b**m−1→b**m. If n≥2m, you are supposed to reverse and merge the shorter one into the longer one to obtain a list like a1→a2→b**m→a3→a4→b**m−1⋯. For example, given one list being 6→7 and the other one 1→2→3→4→5, you must output 1→2→7→3→4→6→5.
Input Specification:
Each input file contains one test case. For each case, the first line contains the two addresses of the first nodes of L1 and L2, plus a positive N (≤105) which is the total number of nodes given. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1
.
Then N lines follow, each describes a node in the format:
Address Data Next
where Address
is the position of the node, Data
is a positive integer no more than 105, and Next
is the position of the next node. It is guaranteed that no list is empty, and the longer list is at least twice as long as the shorter one.
Output Specification:
For each case, output in order the resulting linked list. Each node occupies a line, and is printed in the same format as in the input.
Sample Input:
00100 01000 7
02233 2 34891
00100 6 00001
34891 3 10086
01000 1 02233
00033 5 -1
10086 4 00033
00001 7 -1
Sample Output:
01000 1 02233
02233 2 00001
00001 7 34891
34891 3 10086
10086 4 00100
00100 6 00033
00033 5 -1
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=100010;
struct node {
int address,data,next;
} list[maxn];
int main() {
int n,st1,st2;
vector<node> v1,v2,v;
scanf("%d%d%d",&st1,&st2,&n);
for(int i=0; i<n; i++) {
int addrs;
scanf("%d",&addrs);
list[addrs].address=addrs;
scanf("%d%d",&list[addrs].data,&list[addrs].next);
}
while(st1!=-1) {
v1.push_back(list[st1]);
st1=list[st1].next;
}
while(st2!=-1) {
v2.push_back(list[st2]);
st2=list[st2].next;
}
int len1=v1.size(),len2=v2.size(),index=0;
if(len1>len2) {
reverse(v2.begin(),v2.end());
for(int i=0; i<=len1; i++) {
if(i!=0&&i%2==0&&index<len2) {
v.push_back(v2[index++]);
}
if(i<len1) v.push_back(v1[i]);
}
} else {
reverse(v1.begin(),v1.end());
for(int i=0; i<=len2; i++) {
if(i!=0&&i%2==0&&index<len1) {
v.push_back(v1[index++]);
}
if(i<len2) v.push_back(v2[i]);
}
}
for(int i=0;i<v.size()-1;i++) printf("%05d %d %05d\n",v[i].address,v[i].data,v[i+1].address);
printf("%05d %d -1",v[v.size()-1].address,v[v.size()-1].data);
return 0;
}
1162(25 输出树的后缀表达式)
Postfix Expression(25分)
Given a syntax tree (binary), you are supposed to output the corresponding postfix expression, with parentheses reflecting the precedences of the operators.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤ 20) which is the total number of nodes in the syntax tree. Then N lines follow, each gives the information of a node (the i-th line corresponds to the i-th node) in the format:
data left_child right_child
where data
is a string of no more than 10 characters, left_child
and right_child
are the indices of this node’s left and right children, respectively. The nodes are indexed from 1 to N. The NULL link is represented by −1. The figures 1 and 2 correspond to the samples 1 and 2, respectively.
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4vCmW2Qx-1618461979957)(C:\Users\lxb\Pictures\树.png)]
Output Specification:
For each case, print in a line the postfix expression, with parentheses reflecting the precedences of the operators.There must be no space between any symbols.
Sample Input 1:
8
* 8 7
a -1 -1
* 4 1
+ 2 5
b -1 -1
d -1 -1
- -1 6
c -1 -1
Sample Output 1:
(((a)(b)+)((c)(-(d))*)*)
Sample Input 2:
8
2.35 -1 -1
* 6 1
- -1 4
% 7 8
+ 2 3
a -1 -1
str -1 -1
871 -1 -1
Sample Output 2:
(((a)(2.35)*)(-((str)(871)%))+)
#include<iostream>
#include<string>
using namespace std;
struct node {
string data;
int lc,rc;
} v[21];
string ans;
void postorder(int index) {
bool flag=false;
if((v[index].rc!=-1&&v[index].lc!=-1)||(v[index].lc==-1&&v[index].rc!=-1)||(v[index].lc==-1&&v[index].rc==-1)) {
flag=true;
ans+="(";
}
if(v[index].lc==-1&&v[index].rc!=-1) {
ans+=v[index].data;
postorder(v[index].rc);
} else {
if(v[index].lc!=-1)postorder(v[index].lc);
if(v[index].rc!=-1)postorder(v[index].rc);
ans+=v[index].data;
}
if(flag) ans+=")";
}
int main() {
int n,root=1,notroot[21]= {0};
cin>>n;
for(int i=1; i<=n; i++) {
cin>>v[i].data>>v[i].lc>>v[i].rc;
if(v[i].lc!=-1) notroot[v[i].lc]=1;
if(v[i].rc!=-1) notroot[v[i].rc]=1;
}
while(notroot[root]==1) root++;
postorder(root);
cout<<ans;
return 0;
}
1163(30 判断dijkst的合法路径)
Dijkstra Sequence (30分)
Dijkstra’s algorithm is one of the very famous greedy algorithms. It is used for solving the single source shortest path problem which gives the shortest paths from one particular source vertex to all the other vertices of the given graph. It was conceived by computer scientist Edsger W. Dijkstra in 1956 and published three years later.
In this algorithm, a set contains vertices included in shortest path tree is maintained. During each step, we find one vertex which is not yet included and has a minimum distance from the source, and collect it into the set. Hence step by step an ordered sequence of vertices, let’s call it Dijkstra sequence, is generated by Dijkstra’s algorithm.
On the other hand, for a given graph, there could be more than one Dijkstra sequence. For example, both { 5, 1, 3, 4, 2 } and { 5, 3, 1, 2, 4 } are Dijkstra sequences for the graph, where 5 is the source. Your job is to check whether a given sequence is Dijkstra sequence or not.
Input Specification:
Each input file contains one test case. For each case, the first line contains two positive integers N**v (≤103) and N**e (≤105), which are the total numbers of vertices and edges, respectively. Hence the vertices are numbered from 1 to N**v.
Then N**e lines follow, each describes an edge by giving the indices of the vertices at the two ends, followed by a positive integer weight (≤100) of the edge. It is guaranteed that the given graph is connected.
Finally the number of queries, K, is given as a positive integer no larger than 100, followed by K lines of sequences, each contains a permutationof the N**v vertices. It is assumed that the first vertex is the source for each sequence.
All the inputs in a line are separated by a space.
Output Specification:
For each of the K sequences, print in a line Yes
if it is a Dijkstra sequence, or No
if not.
Sample Input:
5 7
1 2 2
1 5 1
2 3 1
2 4 1
2 5 2
3 5 1
3 4 1
4
5 1 3 4 2
5 3 1 2 4
2 3 4 5 1
3 2 1 5 4
Sample Output:
Yes
Yes
Yes
No
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=1001;
const int INF=0x7fffffff;
int g[maxn][maxn],d[maxn],nv,ne,k,index;
bool vis[maxn];
vector<int> path;
bool judge(int s) {
fill(vis,vis+maxn,false);
fill(d,d+maxn,INF);
d[s]=0;
for(int i=1; i<=nv; i++) {
int u=-1,MIN=INF;
for(int j=1; j<=nv; j++) {
if(vis[j]==false&&d[j]<MIN) {
MIN=d[j];
u=j;
}
}
if(u==-1) return true;
if(d[path[index]]==MIN&&vis[path[index]]==false) {
u=path[index];
index++;
vis[u]=true;
} else return false;
for(int v=1; v<=nv; v++) {
if(vis[v]==false&&g[u][v]!=INF) {
if(d[v]>d[u]+g[u][v]) d[v]=d[u]+g[u][v];
}
}
}
}
int main() {
scanf("%d%d",&nv,&ne);
fill(g[0],g[0]+maxn*maxn,INF);
for(int i=0; i<ne; i++) {
int v1,v2;
scanf("%d%d",&v1,&v2);
scanf("%d",&g[v1][v2]);
g[v2][v1]=g[v1][v2];
}
scanf("%d",&k);
while(k--) {
path.clear();
path.resize(nv);
for(int i=0; i<nv; i++) scanf("%d",&path[i]);
index=0;
if(judge(path[index])) printf("Yes\n");
else printf("No\n");
}
return 0;
}