A題:ios
A題題目連接
git
題目描寫敘述:app
已知一個包括 n 個元素的正整數集合S。設 f(S) 爲集合S中所有元素的異或(XOR)的結果。less
如:S={1,2,3}, 則 f(S) = 0。jsp
給出集合S,你需要計算 將所有f(s)進行異或後的值, 這裏 s⊆S.ide
多組測試數據。第一行包括一個整數T(T≤20) 表示組數。函數
每組測試數據第一行包括一個數 n(1≤n≤1,000) 表示集合的大小,第二行爲 n個數表示集合post
元素。第 i(1≤i≤n) 個數 0 ≤ai ≤1000,000,000 且數據保證所給集合中沒有反覆元素。ui
對於每組測試數據,輸出一個數,表示將所有的 f(s)異或以後的值。this
1 3 1 2 3
0 例子中,S={1,2,3}, 它的子集有∅, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}
這道題乍看上去讓人摸不着頭腦,事實上倒是一道很easy的題目。
一開始作題目沒有頭緒的時候。咱們可以從簡單枚舉開始。從而
尋找通常規律。
首先,需要了解的是一些異或運算的基本性質:
異或運算是知足結合律和交換律的,可以用卡諾圖和真值表進行證實,這裏就不細說了。
當集合S中僅僅有一個元素的時候,子集僅僅有空集和其自己。很是明顯。所有f(s)異或後的值就是其自己
當集合S中有兩個元素{a1,a2}的時候,子集有空集,{a1},{a2},{a1。a2}。那麼所有集合進行異或運算的結果即爲:
a1 ^ a2 ^ a1 ^ a2 = a1 ^ a1 ^ a2 ^ a2 = 0
當集合S中有三個元素{a1,a2,a3}的時候,子集有空集。{a1},{a2},{a3},{a1,a2},{a1。a3},{a2,a3}。{a1,a2。a3}。
那麼所
有集合進行異或運算的結果即爲:
a1 ^ a2 ^ a3 ^ a1 ^ a2 ^ a1 ^ a3 ^ a2 ^ a3 ^ a1 ^ a2 ^ a3= a1 ^ a1 ^ a1 ^ a1 ^ a2 ^ a2 ^ a2 ^ a2 ^ a3 ^ a3^ a3 ^ a3 = 0
枚舉到這裏,所以咱們可以猜測,集合S中元素個數大於1時。所有子集異或後的結果爲0。不然則爲其自己。
簡單證實:
若是集合S中有n個元素,對於當中的任一元素x,則總共2^(n-1)個子集包括元素x(排列組合證實,這裏不具體說。有興趣的可
以本身證實。結合二項式定理),那麼當n>1時,x在所有子集中個數之和爲偶數。那麼所有x異或後的結果必爲0,當n = 1時,則
特判僅僅含有一個元素的狀況。
完整代碼實現:
#include<cstdio> int main(){ int T,n,value; scanf("%d",&T); while(T--){ scanf("%d",&n); for(int i = 1;i <= n;++i){ scanf("%d",&value); } if(n==1){ printf("%d\n",value); } else{ printf("0\n"); } } }
題目描寫敘述:
You are given two very long integers a, b (leading zeroes are allowed). You should check what number a or b is greater or determine that they are equal.
The input size is very large so don't use the reading of symbols one by one. Instead of that use the reading of a whole line or token.
As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to usescanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. Don't use the function input() in Python2 instead of it use the function raw_input().
The first line contains a non-negative integer a.
The second line contains a non-negative integer b.
The numbers a, b may contain leading zeroes. Each of them contains no more than 106 digits.
Print the symbol "<" if a < b and the symbol ">" if a > b. If the numbers are equal print the symbol "=".
9
10
<
11
10
>
00012345
12345
=
0123
9
>
0123
111
>
給定兩個整數,這兩個整數可能含有前綴0。而後比較這兩個數的大小,輸出對應符號(< > =)
解析:
用字符串處理,分三種狀況,除去前綴0以後。長者更大。反之短者更小。相同長則調用strcmp函數推斷。
完整代碼實現:
#include <cstdio> #include <cstring> char s[1000001], t[1000001]; int main() { scanf("%s %s", s, t); int n = 0, m = 0; while (s[n] == '0') n++; while (t[m] == '0') m++; int l1 = strlen(s + n); int l2 = strlen(t + m); if (l1 < l2) puts("<"); else if (l1 > l2) puts(">"); else { int cmp = strcmp(s + n, t + m); puts(cmp > 0 ?">" : (cmp == 0 ? "=" : "<")); } return 0; }
題目描寫敘述:
Luke Skywalker got locked up in a rubbish shredder between two presses. R2D2 is already working on his rescue, but Luke needs to stay alive as long as possible. For simplicity we will assume that everything happens on a straight line, the presses are initially at coordinates 0 and L, and they move towards each other with speed v1 and v2, respectively. Luke has width d and is able to choose any position between the presses. Luke dies as soon as the distance between the presses is less than his width. Your task is to determine for how long Luke can stay alive.
The first line of the input contains four integers d, L, v1, v2 (1 ≤ d, L, v1, v2 ≤ 10 000, d < L) — Luke's width, the initial position of the second press and the speed of the first and second presses, respectively.
Print a single real value — the maximum period of time Luke can stay alive for. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .
2 6 2 2
1.00000000000000000000
1 9 1 2
2.66666666666666650000
NoteIn the first sample Luke should stay exactly in the middle of the segment, that is at coordinates [2;4], as the presses move with the same speed.
In the second sample he needs to occupy the position . In this case both presses move to his edges at the same time.
兩我的分別從A,B兩地相向而行,當兩我的的距離達到d時,中止運動,問此時兩我的運動的時間,距離L,兩人速度v1,v2均給出。
解析:
相遇問題,列個一元一次方程求解就能夠
完整代碼實現:
#include<cstdio> int main(){ int d,L,v1,v2; while(scanf("%d %d %d %d",&d,&L,&v1,&v2)==4){ printf("%.20f\n",double(L-d)/(v1+v2)); } return 0; }
Vasya lives in a round building, whose entrances are numbered sequentially by integers from 1 to n. Entrance n and entrance 1 are adjacent.
Today Vasya got bored and decided to take a walk in the yard. Vasya lives in entrance a and he decided that during his walk he will move around the house b entrances in the direction of increasing numbers (in this order entrance n should be followed by entrance 1). The negative value of b corresponds to moving |b| entrances in the order of decreasing numbers (in this order entrance 1 is followed by entrance n). If b = 0, then Vasya prefers to walk beside his entrance.
Help Vasya to determine the number of the entrance, near which he will be at the end of his walk.
The single line of the input contains three space-separated integers n, a and b (1 ≤ n ≤ 100, 1 ≤ a ≤ n, - 100 ≤ b ≤ 100) — the number of entrances at Vasya's place, the number of his entrance and the length of his walk, respectively.
Print a single integer k (1 ≤ k ≤ n) — the number of the entrance where Vasya will be at the end of his walk.
6 2 -5
3
5 1 3
4
3 2 7
3
NoteThe first example is illustrated by the picture in the statements.
給定n,a,b分別表示入口的個數,初始位置以及要走過的路口數。b值爲負時逆時針走動,不然則順時針走動。
問最後走到的路口
的標號。
解析:
直接模擬題意就能夠,注意防止標號爲負。
完整代碼實現:
#include<cstdio> int main(){ int n,a,b; while(scanf("%d %d %d",&n,&a,&b)==3){ printf("%d\n",(a+100*n+b)%n ? (a+100*n+b)%n : n); } return 0; }
題目描寫敘述:
Bessie the cow and her best friend Elsie each received a sliding puzzle on Pi Day. Their puzzles consist of a 2 × 2 grid and three tiles labeled 'A', 'B', and 'C'. The three tiles sit on top of the grid, leaving one grid cell empty. To make a move, Bessie or Elsie can slide a tile adjacent to the empty cell into the empty cell as shown below:
In order to determine if they are truly Best Friends For Life (BFFLs), Bessie and Elsie would like to know if there exists a sequence of moves that takes their puzzles to the same configuration (moves can be performed in both puzzles). Two puzzles are considered to be in the same configuration if each tile is on top of the same grid cell in both puzzles. Since the tiles are labeled with letters, rotations and reflections are not allowed.
The first two lines of the input consist of a 2 × 2 grid describing the initial configuration of Bessie's puzzle. The next two lines contain a 2 × 2 grid describing the initial configuration of Elsie's puzzle. The positions of the tiles are labeled 'A', 'B', and 'C', while the empty cell is labeled 'X'. It's guaranteed that both puzzles contain exactly one tile with each letter and exactly one empty position.
Output "YES"(without quotes) if the puzzles can reach the same configuration (and Bessie and Elsie are truly BFFLs). Otherwise, print "NO" (without quotes).
AB
XC
XB
AC
YES
AB
XC
AC
BX
NO
NoteThe solution to the first sample is described by the image. All Bessie needs to do is slide her 'A' tile down.
In the second sample, the two puzzles can never be in the same configuration. Perhaps Bessie and Elsie are not meant to be friends after all...
給定兩個2*2的宮格,問左邊的宮格經過若干次移動以後,是否能夠移成右邊宮格的形狀。
解析:
由於宮格僅僅有2*2。直接暴力模擬就能夠。
完整代碼實現:
#include<iostream> #include<string> #include<algorithm> using namespace std; string a[2], b[2]; int main() { cin >> a[0] >> a[1] >> b[0] >> b[1]; for (int i = 0; i < 1000; ++i) { if (a[0] == b[0] && a[1] == b[1]) { cout << "YES"; return 0; } if (a[0][0] == 'X') swap(a[0][0], a[0][1]); else if (a[0][1] == 'X') swap(a[0][1], a[1][1]); else if (a[1][1] == 'X') swap(a[1][1], a[1][0]); else if (a[1][0] == 'X') swap(a[1][0], a[0][0]); } cout << "NO"; return 0; }
題目描寫敘述:
Limak and Radewoosh are going to compete against each other in the upcoming algorithmic contest. They are equally skilled but they won't solve problems in the same order.
There will be n problems. The i-th problem has initial score pi and it takes exactly ti minutes to solve it. Problems are sorted by difficulty — it's guaranteed that pi < pi + 1 and ti < ti + 1.
A constant c is given too, representing the speed of loosing points. Then, submitting the i-th problem at time x (x minutes after the start of the contest) gives max(0, pi - c·x) points.
Limak is going to solve problems in order 1, 2, ..., n (sorted increasingly by pi). Radewoosh is going to solve them in ordern, n - 1, ..., 1 (sorted decreasingly by pi). Your task is to predict the outcome — print the name of the winner (person who gets more points at the end) or a word "Tie" in case of a tie.
You may assume that the duration of the competition is greater or equal than the sum of all ti. That means both Limak and Radewoosh will accept all n problems.
The first line contains two integers n and c (1 ≤ n ≤ 50, 1 ≤ c ≤ 1000) — the number of problems and the constant representing the speed of loosing points.
The second line contains n integers p1, p2, ..., pn (1 ≤ pi ≤ 1000, pi < pi + 1) — initial scores.
The third line contains n integers t1, t2, ..., tn (1 ≤ ti ≤ 1000, ti < ti + 1) where ti denotes the number of minutes one needs to solve the i-th problem.
Print "Limak" (without quotes) if Limak will get more points in total. Print "Radewoosh" (without quotes) if Radewoosh will get more points in total. Print "Tie" (without quotes) if Limak and Radewoosh will get the same total number of points.
3 2
50 85 250
10 15 25
Limak
3 6
50 85 250
10 15 25
Radewoosh
8 1
10 20 30 40 50 60 70 80
8 10 58 63 71 72 75 76
Tie
NoteIn the first sample, there are 3 problems. Limak solves them as follows:
- Limak spends 10 minutes on the 1-st problem and he gets 50 - c·10 = 50 - 2·10 = 30 points.
- Limak spends 15 minutes on the 2-nd problem so he submits it 10 + 15 = 25 minutes after the start of the contest. For the 2-nd problem he gets 85 - 2·25 = 35 points.
- He spends 25 minutes on the 3-rd problem so he submits it 10 + 15 + 25 = 50 minutes after the start. For this problem he gets 250 - 2·50 = 150 points.
So, Limak got 30 + 35 + 150 = 215 points.
Radewoosh solves problem in the reversed order:
- Radewoosh solves 3-rd problem after 25 minutes so he gets 250 - 2·25 = 200 points.
- He spends 15 minutes on the 2-nd problem so he submits it 25 + 15 = 40 minutes after the start. He gets 85 - 2·40 = 5 points for this problem.
- He spends 10 minutes on the 1-st problem so he submits it 25 + 15 + 10 = 50 minutes after the start. He gets max(0, 50 - 2·50) = max(0, - 50) = 0 points.
Radewoosh got 200 + 5 + 0 = 205 points in total. Limak has 215 points so Limak wins.
In the second sample, Limak will get 0 points for each problem and Radewoosh will first solve the hardest problem and he will get 250 - 6·25 = 100 points for that. Radewoosh will get 0 points for other two problems but he is the winner anyway.
In the third sample, Limak will get 2 points for the 1-st problem and 2 points for the 2-nd problem. Radewoosh will get 4 points for the 8-th problem. They won't get points for other problems and thus there is a tie because 2 + 2 = 4.
由於在題目後方,題意已經解釋得很清楚。所以題意再也不多說。事實上這就是codeforces本地比賽的規則(codeforces daily 、
round)。所以在處理從前日後作題和從後往前作題的兩種狀況時,咱們分別可以用後綴和以及前綴和先預處理一下就能夠。
完整代碼實現:
#include<cstdio> #include<algorithm> using namespace std; const int maxn = int(1e3) + 10; int p[maxn],t[maxn],t1[maxn],t2[maxn]; int main(){ int n,c; while(scanf("%d %d",&n,&c)==2){ for(int i = 1;i <= n;++i){ scanf("%d",&p[i]); } for(int i = 1;i <= n;++i){ scanf("%d",&t[i]); t1[i] = t[i]; t2[i] = t[i]; } for(int i = 1;i < n;++i){ t1[i+1] += t1[i]; } for(int i = n;i > 1;--i){ t2[i-1] += t2[i]; } int score1 = 0,score2 = 0; for(int i = 1;i <= n;++i){ score1 += max(0,p[i]-c*t1[i]); } for(int i = n;i >= 1;--i){ score2 += max(0,p[i]-c*t2[i]); } if(score1 > score2){ printf("Limak\n"); } else if(score1 < score2){ printf("Radewoosh\n"); } else{ printf("Tie\n"); } } return 0; }
若有錯誤,還請指正,O(∩_∩)O謝謝