A:24 Game
Test instructions: Give an n, ask from 1 to n by subtraction to get 24 of the method.
Solution: It is not difficult to think of n<4 time must be without solutions,
N=4 Time 1+2=3 3+3=6 6*4=24
N=5 time 1*5=5 5-2=3 3+3=6 6*4=24
When n>5 we can let N-(n-1) =1 1*1=1 so 1~n into 1~n-2
So for any given n, we will change the number of (5~n) in the adjacent subtraction into 1 final conversion with the 1~4 or the structure of the answer can be.
1#include <cstdio>2 3#include <cmath>4 5#include <iostream>6 7#include <algorithm>8 9#include <cstring>Ten One A - using namespacestd; - the intN; - - voidWorkintx) - + { - + if(x==4) A at { - -printf"1 + 2 = 3\n3 + 3 = 6\n4 * 6 = 24\n"); - - return; - in } - to if(x==5) + - { the *printf"1 * 5 = 5\n5-2 = 3\n3 + 3 = 6\n6 * 4 = 24\n"); $ Panax Notoginseng return; - the } + Aprintf"%d-%d = 1\n1 * 1 = 1\n", x,x-1); the +Work (X-2); - $ } $ - intMain () - the { - Wuyiscanf"%d",&n); the - if(n<4) Wu - { About $printf"no\n"); - - return 0; - A } + theprintf"yes\n"); - $ Work (n); the the return 0; the the}
View Code
B:two Sets
Test instructions: Given n number P1~PN requirements to divide this n number into two sets, the requirement if X∈a a-x∈a if X∈b is b-x for given.
Puzzle: Use a map to record whether each number has occurred, if for a pi A-pi is not in this number, we can only put Pi into the B set, so with a queue record must appear in the B set number,
Each time the team head element X is removed, check whether the b-x appears in P, if it appears to join this queue, and then join the b-x at the same time easy to know A-(b-x) must also appear in set B, but also to join the queue.
1#include <cstdio>2 3#include <iostream>4 5#include <algorithm>6 7#include <cmath>8 9#include <cstring>Ten One#include <queue> A -#include <map> - the using namespacestd; - -map<int,int>Pos; - + intn,a,b,ans[500000],x[500000]; - + intMain () A at { - -queue<int>Q; - -scanf"%d%d%d",&n,&a,&b); - in for(intI=1; i<=n;++i) - to { + -scanf"%d",&x[i]); the *pos[x[i]]=i; $ Panax Notoginseng } - the for(intI=1; i<=n;++i) + A { the + if(pos[a-x[i]]==0) - $ { $ -ans[i]=1; - the Q.push (X[i]); - Wuyi } the - } Wu - while(!q.empty ()) About $ { - - intnow=Q.front (); - A Q.pop (); + the if(pos[b-now]==0) - $ { the theprintf"no\n"); the the return 0; - in } the the if(ans[pos[b-now]]==1)Continue; About theans[pos[b-now]]=1; the the if(pos[a-b+now]!=0) + - { the Bayians[pos[a-b+now]]=1; the theQ.push (a-b+Now ); - - } the the } the theprintf"yes\n"); - the for(intI=1; i<=n;++i) printf ("%d", Ans[i]); the the return 0;94 the}
View Code
C:hack it!
Test instructions: Defines the sum of the numbers f (x) for x, such as f (1234) =1+2+3+4=10, which is calculated by one of the following algorithms
sum=
ans = Solve (l, r)% A;
if (ans <= 0)
ans + = A;
When the ans=0 error occurs, we construct a l,r to hack the given a.
Gy god Ben told me.
Let's look at the sum value of l=1 r=1e18.
L R When I was +1 we found that the sum value was added to 1! Because R plus 1 after the next few is the number of L does not add 1 times, so each add 1 is the whole sequence forward to push a bit, and then r into 1000 ... L So l,r each add 1,sum add 1, so first find out l=1 r=1e18 sum value after move (a-sum) times can! R is large enough to do this without having to consider the rounding problem, otherwise R is not 1 at the front, and there is no guarantee that L will appear behind. Cleverly constructed ~
Algorithm for sum: Sum value of ai= (1~10^i)-1
A1=45
A2=10*a1+45*10
...
An=10*an-1+45*10^i
Get an=45*n*10^ (n-1)
A18=81*1e18
So sum= (a18+1)%a; the problem is solved.
1#include <cstdio>2 3#include <cmath>4 5#include <iostream>6 7#include <cstring>8 9#include <algorithm>Ten One A - using namespacestd; - thetypedefLong LongLL; - - LL L,r,a,ans; - + intMain () - + { A atL=1; r=1e18; - -scanf"%i64d",&a); - -Ans= ((3*(3*(3*(3*(5*(2* (LL) 1e17%a))%a)%a)%a)%a)%a)%a+1)%A; - inprintf"%i64d%i64d", l+a-ans,r+a-ans); - to return 0; + -}
View Code
Codeforces Round #268 (Div. 1)