A Choosing Ice Cream
You are standing in the supermarket in front of the freezers. You have a very tough task ahead of you: you have to choose what type of ice cream you want for after dinner that evening. After a while, you give up: they are all awesome! Instead, you take your (fair) kk-sided die out of your pocket and you decide to let fate decide.
Of course, the number of ice cream choices, nn, may not be precisely kk, in which case you could not just throw the die once, rolling ii, and take the iith ice cream choice. You therefore have to use some algorithm that involves zero or more die throws that results in an ice cream choice with every choice being exactly equally likely. Being a good computer scientist, you know about the accept-reject method, which would let you make such a fair choice.
At that point, you remember that you have a very importantcompetition to attend that same afternoon. You absolutely cannot afford to be late for that competition. Because of this, you decide you cannot use the accept-reject method, as there may be no bound on the number of die throws needed to ensure a fair result, so you may end up standing there for a long time and miss the competition! Instead, you resolve to find an algorithm that is fair and uses as few dice choices as possible in the worst case.
Given nn and kk, can you determine the minimum number ii such that there is a fair algorithm that uses at most iidie throws per execution?
Input Format
On the first line one positive number: the number of test cases, at most 100. After that per test case:
- one line with two space-separated integers nn and kk (1 \leq n, k \leq 10^91≤n,k≤109): the number of ice cream choices and the number of sides of your die, respectively.
Output Format
Per test case:
- one line with a single integer: the smallest number of throws after which you are guaranteed to be able to make a fair choice. If there is no such number, print “unbounded” instead.
样例输入
3 4 2 2 4 3 2
样例输出
2 1 unbounded
题目来源
求k^x%n==0中最小的x
1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 const int N = 110; 5 ll n, t, k; 6 ll fun(ll i) { 7 ll ans = 1%n; 8 while(i--) { 9 ans *= k; 10 ans %= n; 11 } 12 return ans; 13 } 14 int main() { 15 scanf("%lld",&t); 16 while(t--) { 17 scanf("%lld%lld",&n,&k); 18 bool flag = false; 19 ll i = 0; 20 for(ll j = 1; j <= n; j*=2) { 21 if(fun(i) == 0) { 22 printf("%lld\n",i); 23 flag = true; 24 break; 25 } 26 i++; 27 } 28 if(!flag) printf("unbounded\n"); 29 } 30 return 0; 31 }
B Failing Components
As a jury member of the Best Architectural Planning Contest, you are tasked with scoring the reliability of a system. All systems entered in the contest consist of a number of components which depend on each other. The reliability of such a system depends on the damage done by a failing component. Ideally a failing component should have no consequences, but since most components depend on each other, some other components will usually fail as well.
Most components are somewhat resilient to short failures of the components they depend on. For example, a database could be unavailable for a minute before the caches expire and new data must be retrieved from the database. In this case, the caches can survive for a minute after a database failure, before failing themselves. If a component depends on multiple other components which fail, it will fail as soon as it can no longer survive the failure of at least one of the components it depends on. Furthermore no component depends on itself directly, however indirect self-dependency through other components is possible.
You want to know how many components will fail when a certain component fails, and how much time passes before all components that will eventually fail, actually fail. This is difficult to calculate by hand, so you decided to write a program to help you. Given the description of the system, and the initial component that fails, the program should report how many components will fail in total, and how much time passes before all those components have actually failed.
Input Format
On the first line one positive number: the number of test cases, at most 100. After that per test case:
- one line with three space-separated integers nn, dd and cc (1\leq n \leq 100001≤n≤10000 and 1 \leq d \leq 1000001≤d≤100000 and 1 \leq c \leq n1≤c≤n): the total number of components in the system, the number of dependencies between components, and the initial component that fails, respectively.
- dd lines with three space-separated integers aa, bb and ss (1 \leq a,b \leq n1≤a,b≤n and a\ != ba !=b and 0 \leq s \leq 1 0000≤s≤1000), indicating that component aa depends on component bb, and can survive for ss seconds when component bbfails.
In each test case, all dependencies (a, b)(a,b) are unique.
Output Format
Per test case:
- one line with two space-separated integers: the total number of components that will fail, and the number of seconds before all components that will fail, have actually failed.
样例输入
2 3 2 2 2 1 5 3 2 5 3 3 1 2 1 2 3 1 8 3 2 4
样例输出
2 5 3 6
题目来源
n个组件,d个依赖关系,初试坏的组件为c。依赖关系为a依赖b,在b坏了后a还能运行s秒。求在c坏了后,最后有多少个组件损坏,用的时间是多少秒。
可以转换成最短路径来做。
1 #include <bits/stdc++.h> 2 #define INF 0x3f3f3f3f 3 #define ll long long 4 using namespace std; 5 const int N = 10010; 6 typedef pair<int, int> P; 7 struct Nod{ 8 int to, w; 9 }; 10 vector<Nod> vs[N]; 11 int dist[N]; 12 13 void bfs(int s) { 14 priority_queue<P,vector<P>,greater<P> > que; 15 dist[s] = 0; 16 que.push(P(0, s)); 17 while(!que.empty()) { 18 P p = que.top(); que.pop(); 19 int v = p.second; 20 if(dist[v] < p.first) continue; 21 for(int i = 0; i < vs[v].size(); i ++) { 22 Nod e = vs[v][i]; 23 if(dist[e.to] > dist[v] + e.w) { 24 dist[e.to] = dist[v] + e.w; 25 que.push(P(dist[e.to], e.to)); 26 } 27 } 28 } 29 } 30 int main() { 31 int t, n, d, c; 32 scanf("%d", &t); 33 while(t--) { 34 memset(dist, INF, sizeof(dist)); 35 scanf("%d%d%d", &n, &d, &c); 36 for(int i = 0; i < d; i ++) { 37 int a, b, s; 38 Nod e; 39 scanf("%d%d%d", &a, &b, &s); 40 e.to = a, e.w = s; 41 vs[b].push_back(e); 42 } 43 bfs(c); 44 int cnt = 0, MIN = 0; 45 for(int i = 1; i <= n; i ++) { 46 if(dist[i] != INF) { 47 cnt++; 48 MIN = max(MIN, dist[i]); 49 } 50 } 51 for(int i = 1; i <= n; i ++) { 52 vs[i].clear(); 53 } 54 printf("%d %d\n",cnt,MIN); 55 } 56 return 0; 57 }
F Runway Planning
Most airports have multiple runways. To identify runways, they are given a number indicat- ing the direction of the runway. Such a runway number is obtained by dividing the heading of the runway in degrees by ten, rounding the result, and optionally prefixing it with a ‘0’ if the result has only a single digit. For example, a runway with a heading of 82° is indicated by the number 08.If you are paying attention, you might think “a runway can be used in both directions, and therefore has two headings, but it is only assigned one runway number.” You are correct: normally a runway is identified by two numbers, based on the direction in which the runway is used. To simplify matters, we only concern ourselves with the smallest of these two num- bers, except if it is zero; we then use 18 instead. The runway numbers thus range from 01 to 18.
Now, you might think, “what if two runways have the same heading?” In that case, the characters ‘L’ and ‘R’ are appended to the number of the left and right runway, respectively. But what if three runways have the same heading? Then, the character ‘C’ is appended to the center runway. “But”, I can hear you ask, “what if four runways have the same heading?” If you really want to know, look up how Dallas/Fort Worth International Airport solved this problem after the contest. At any rate, we do not concern ourselves with multiple runways having the same heading in this problem, so you can forget all you read in this paragraph.
The runway in use is mostly determined by the current direction of the wind. It is pre- ferred to take off and land with headwind. If it is not possible to have the wind coming from straight ahead, its direction should be as close to that as possible. For example, if an airport has the runways 05 and 15, and the wind has a heading of 70°, taking off and landing using runway 05 is preferred, since the heading of that runway is closest to the heading of the wind.
Now, consider an airport already having one or more runways, and planning the con- struction of a new runway. Obviously, this runway should have a unique runway number: not only would we otherwise have a problem outside the boundaries of our restricted runway numbering outlined above, but, most importantly, this increases the probability of being able to take off or land with headwind.
The engineers at the airport under consideration have already determined the heading of the new runway, but still need you to determine the runway number. Note that the engineers are not very considerate with their input to your problem. They give you one heading of the runway, but it can be either the lowest or the highest heading of the runway. Be sure to give the lowest of the two runway numbers, as discussed in the second paragraph of this problem statement, even if you are given the highest of the two headings from the engineers.
输入格式
On the first line one positive number: the number of test cases, at most 100. After that per test case:
- one line with a single integer hh (1 \leq h \leq 3601≤h≤360): the heading of the new runway in degrees.
输出格式
Per test case:
- one line with the runway number of the newly constructed runway.
样例输入
4 82 115 316 4
样例输出
08 12 14 18
题目来源
签到题。
1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 const int N = 110; 5 6 int main() { 7 int t, n; 8 cin >> t; 9 while(t--) { 10 cin >> n; 11 if(n > 180) n -= 180; 12 n = int(1.0*n/10+0.5); 13 if(n == 0) n = 18; 14 printf("%02d\n",n); 15 } 16 return 0; 17 }
原文地址:https://www.cnblogs.com/xingkongyihao/p/9279813.html