输入一个数n求是否有abcde/fghij=n,abcdefghij都是不同的数字,有的全部输出,没有的就按格式输出。
暴力可以做时间为10!。
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 #include <iomanip> 6 #define ll long long 7 using namespace std; 8 int n, k, cnt, cnt1,res; 9 int vis[1100]; 10 11 int main(){ 12 int kk = 1; 13 while(scanf("%d",&n)&&n) { 14 memset(vis,false,sizeof(vis)); 15 int a,b,c,d,e,f,g,h,ii,jj,k = 0; 16 if(kk!=1)printf("\n"); 17 for(int i = 1234; i <= 98765; i ++) { 18 memset(vis,0,sizeof(vis)); 19 a = i%10,b = i/10%10,c = i/100%10,d = i/1000%10,e = i/10000%10; 20 int num = i*n; 21 if(num > 98765)break; 22 f = num%10,g = num/10%10,h = num/100%10,ii = num/1000%10,jj = num/10000%10; 23 vis[a]++;vis[b]++;vis[c]++;vis[d]++;vis[e]++; 24 vis[f]++;vis[g]++;vis[h]++;vis[ii]++;vis[jj]++; 25 int j; 26 for(j = 0; j < 10; j ++) { 27 if(vis[j] > 1)break; 28 } 29 if(j == 10) { 30 printf("%d%d%d%d%d / %d%d%d%d%d = %d\n",jj,ii,h,g,f,e,d,c,b,a,n); 31 k++; 32 }else continue; 33 } 34 if(k == 0) printf("There are no solutions for %d.\n",n); 35 kk++; 36 } 37 return 0; 38 }
电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额。如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负),否则无法购买(即使金额足够)。所以大家都希望尽量使卡上的余额最少。
某天,食堂中有n种菜出售,每种菜可购买一次。已知每种菜的价格以及卡上的余额,问最少可使卡上的余额为多少。
Input多组数据。对于每组数据:
第一行为正整数n,表示菜的数量。n<=1000。
第二行包括n个正整数,表示每种菜的价格。价格不超过50。
第三行包括一个正整数m,表示卡上的余额。m<=1000。
n=0表示数据结束。
Output对于每组输入,输出一行,包含一个整数,表示卡上可能的最小余额。Sample Input
1 50 5 10 1 2 3 2 1 1 2 3 2 1 50 0
Sample Output
-45 32
背包问题,原题,先排序让最大的一个不做计算,剩下的求出可以组成最大不超过m-5的数,在把它减去不做计算的那个最大数就是答案了。
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 using namespace std; 6 int v[1010]; 7 int dp[1010]; 8 int main(){ 9 int n,m,p; 10 while(scanf("%d",&n)&&n){ 11 for(int i = 0; i < n; i ++)scanf("%d",&v[i]); 12 scanf("%d",&m); 13 sort(v,v+n); 14 for(int i = 0; i < n-1; i ++){ 15 for(int k = m-5; k >= v[i]; k--){ 16 dp[k] = max(dp[k],dp[k-v[i]]+v[i]); 17 } 18 } 19 if(m < 5)printf("%d\n",m); 20 else printf("%d\n",m-dp[m-5]-v[n-1]); 21 memset(dp,0,sizeof(dp)); 22 memset(v,0,sizeof(v)); 23 } 24 return 0; 25 }
Comrade Dujikov is busy choosing artists for Timofey‘s birthday and is recieving calls from Taymyr from Ilia-alpinist.
Ilia-alpinist calls every n minutes, i.e. in minutes n, 2n, 3n and so on. Artists come to the comrade every m minutes, i.e. in minutes m, 2m, 3m and so on. The day is z minutes long, i.e. the day consists of minutes 1,?2,?...,?z. How many artists should be killed so that there are no artists in the room when Ilia calls? Consider that a call and a talk with an artist take exactly one minute.
Input
The only string contains three integers — n, m and z (1?≤?n,?m,?z?≤?104).
Output
Print single integer — the minimum number of artists that should be killed so that there are no artists in the room when Ilia calls.
Example
Input
1 1 10
Output
10
Input
1 2 5
Output
2
Input
2 3 9
Output
1
Note
Taymyr is a place in the north of Russia.
In the first test the artists come each minute, as well as the calls, so we need to kill all of them.
In the second test we need to kill artists which come on the second and the fourth minutes.
In the third test — only the artist which comes on the sixth minute.
原题。记下在z的范围的即是n的倍数又是m的倍数的数量。
#include <bits/stdc++.h> using namespace std; int a[10010],b[10010]; int main(){ int n, m, z, ans = 0; cin >> n >> m >> z; for(int i = 1; i*n <= z; i++) a[i*n] = 1; for(int i = 1; i*m <= z; i ++) b[i*m] = 1; for(int i = 1; i <= z; i ++){ if(a[i]==1 && b[i]==1) ans++; } cout << ans << endl; return 0; }
Each New Year Timofey and his friends cut down a tree of n vertices and bring it home. After that they paint all the n its vertices, so that the i-th vertex gets color ci.
Now it‘s time for Timofey birthday, and his mother asked him to remove the tree. Timofey removes the tree in the following way: he takes some vertex in hands, while all the other vertices move down so that the tree becomes rooted at the chosen vertex. After that Timofey brings the tree to a trash can.
Timofey doesn‘t like it when many colors are mixing together. A subtree annoys him if there are vertices of different color in it. Timofey wants to find a vertex which he should take in hands so that there are no subtrees that annoy him. He doesn‘t consider the whole tree as a subtree since he can‘t see the color of the root vertex.
A subtree of some vertex is a subgraph containing that vertex and all its descendants.
Your task is to determine if there is a vertex, taking which in hands Timofey wouldn‘t be annoyed.
Input
The first line contains single integer n (2?≤?n?≤?105) — the number of vertices in the tree.
Each of the next n?-?1 lines contains two integers u and v (1?≤?u,?v?≤?n, u?≠?v), denoting there is an edge between vertices u and v. It is guaranteed that the given graph is a tree.
The next line contains n integers c1,?c2,?...,?cn (1?≤?ci?≤?105), denoting the colors of the vertices.
Output
Print "NO" in a single line, if Timofey can‘t take the tree in such a way that it doesn‘t annoy him.
Otherwise print "YES" in the first line. In the second line print the index of the vertex which Timofey should take in hands. If there are multiple answers, print any of them.
Example
Input
41 22 33 41 2 1 1
Output
YES2
Input
31 22 31 2 3
Output
YES2
Input
41 22 33 41 2 1 2
Output
NO 又是一个原题,求是否有一个节点,当它成顶点时,它的每一个子节点下的节点们的颜色都一样。当有时,由于子节点下的节点们的颜色都一样,说明只有顶点和子节点的颜色不同,那么顶点出现的此数和相邻节点颜色不同的数量是一样的。
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 using namespace std; 5 const int N = 1e5+10; 6 int x[N], y[N], h[N], c[N]; 7 int main() { 8 int n; 9 scanf("%d",&n); 10 for(int i = 1; i < n; i ++) scanf("%d %d",&x[i],&y[i]); 11 for(int i = 1; i <= n; i ++) scanf("%d",&c[i]); 12 int m = 0; 13 for(int i = 1; i < n; i ++) { 14 if(c[x[i]] != c[y[i]]){ 15 m++; 16 h[x[i]]++;h[y[i]]++; 17 } 18 } 19 for(int i = 1; i <= n; i ++){ 20 if(h[i] == m){ 21 return 0*printf("YES\n%d\n",i); 22 } 23 } 24 printf("NO\n"); 25 return 0; 26 }
E UVA - 1640
比赛时一直输出很多区间,看规律,看不一个规律,不过没写出来。赛后去搜索了下题解。
题意:
统计两个整数a,b之间各个数字(0~9)出现的次数,如1024和1032,他们之间的数字有1024 1025 1026 1027 1028 1029 1030 1031 1032 总共有10个0,10个1,3个3等等。
分析:
因为前导0的干扰,为了计算方便暂时都先计算在内,之后再减;
如果是0~199,那么百位上的0和1各出现一次,s剩下的就是两个00~99,总共两百个二位数,而每个数出现的次数都一样,都是2*(99-00+1)/10;
那么任意的数都可以分解成类似的数字,如3426,则可以分成0000~2999,3000~3399,3400~3419,3420~3426几个部分各自计算,再求和按位减去前导0的个数。
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 #define ll long long 6 using namespace std; 7 void Cal(char *s, int *num) { 8 int i, j, k, t, m = atoi(s), n = strlen(s); 9 for(i = k = 1; i < n; i ++) 10 k*=10,num[0]-=k; 11 for(i = 0; i < n; i ++, k/=10){ 12 for(j = 0;j < s[i]-‘0‘; j ++) 13 num[j] += k; 14 for(t = 0; t < 10; t ++) 15 num[t] += k/10*(n-i-1)*j; 16 if(i+1 < n) num[j] += atoi(s+i+1); 17 num[j]++; 18 } 19 } 20 int main(){ 21 char s[22]; 22 int n,m,vis[11]={0},vis1[11]={0}; 23 while(scanf("%d%d",&n,&m)){ 24 if(n==0&&m==0)break; 25 if(n>m)swap(n,m); 26 memset(vis,0,sizeof(vis)); 27 sprintf(s,"%d",n-1); 28 Cal(s,vis); 29 memset(vis1,0,sizeof(vis1)); 30 sprintf(s,"%d",m); 31 Cal(s,vis1); 32 for(int i = 0; i < 10; i ++) 33 printf("%d%c",vis1[i]-vis[i],i==9?‘\n‘:‘ ‘); 34 } 35 return 0; 36 }