2016 "Baidu Star"-Preliminary (Astar round2a) Problem Solving report

Source: Internet
Author: User
Tags cmath

This article can use the catalog feature yo ↑ (click above [+])

A little IQ catch urgent, the first problem card for a long time, it seems to defy the old, not stupid is not ... The following is my current problem, what is the question welcome to put forward

Link →2016 "Baidu Star"-Preliminary (Astar round2a)

problem 1001 All X

accept:0 submit:0
Time limit:2000/1000 MSec (java/others) Memory limit:65536 KB

Problem Description

F (X,m) represents an M-digit number that is all composed of a number x. Please calculate whether the following formula is true:

F (x,m) mod k≡c

Input

The first line is an integer t, which represents the T group data. One row for each set of test data, containing four digits x,m,k,c

1≤x≤9

1≤m≤10,000,000,000
?? 0≤c<k≤10,000

Output

For each set of data, output two lines: The first line of output: "Case #i:". I represents the test data for group I. The second line output "Yes" or "No", representing four numbers, whether it can satisfy the formula given in the topic.

Sample INPUT3
1 3 5 2
1 3 5 1
3 5 outputcase Sample #1:
No
Case #2:
Yes
Case #3:
Yes

Hint

For the first set of test data: 111 MoD 5 = 1, the formula is not true, so the answer is "no", and the second set of test data satisfies the above formula, so the answer is "yes".

Problem idea

Problem-solving ideas: This question is M number x composed of m digits to K modulus is equal to C, from the size of M can be seen this problem to use the same remainder theorem 1-bit 1-bit calculation is not solved, so we have to change the idea, first, we extract an X, the rest is M 1, as follows:


This number to mod K, that number should be how to express it?


This translates, and then 10^m can be solved by a quick power, but obviously, divided by 9 How to do, division modulo, the remainder will change, inverse element? But 9 and K are not necessarily coprime, and K is not necessarily prime, so the expansion of Euclid and Fermat theorem can not be used, helpless

Well, here's a little way



So after a few steps of conversion, I beg D do not need division to take the model, then my above problem solved?

Here I would like to mention a bit more optimized (although the optimization effect is not too obvious)

First, we know Euler's theorem.


φ (k) is an Euler function that represents the number of K coprime or less

And we can use Euler's theorem to power down when we're solving 10^m.


Sorry, the above Euler theorem of power misled everyone, Euler theorem is conditional, that is, a and K need coprime, the subject 10 and K not necessarily coprime

Can only say the problem data only water, I thought ill-conceived, but to remove the Euler theorem this part, the code is still right, power down this little optimization can also rely on the rapid power to compensate, it does not matter

Topic link →HDU 5690 all X

Source Code

/*sherlock and Watson and adler*/#pragma comment (linker, "/stack:1024000000,1024000000") #include <stdio.h># include<string.h> #include <stdlib.h> #include <queue> #include <stack> #include <math.h > #include <vector> #include <map> #include <set> #include <cmath> #include <complex># include<string> #include <algorithm> #include <iostream> #define EXP 1e-10using namespace Std;const int N = 100005;const int M = 40;const int inf = 100000000;//const int mod = 2009;__int64 quick_mod (__int64 A, __int64 B, _    _int64 MoD) {__int64 res = 1,term = a% mod;        while (b) {if (b & 1) res = (res * term)% MoD;        term = (term * term)% MoD;    b >>= 1; } return res;    /*__int64 Euler (__int64 N) {__int64 Ans=1;    __int64 i;            for (i=2;i*i<=n;i++) {if (n%i==0) {n/=i;            Ans*=i-1;                while (n%i==0) {n/=i;           Ans*=i; }}} if (n>1) ans*=n-1; return ans;}    */int Main () {int t,p=1,i;    __int64 m,ans,x,c,k;    scanf ("%d", &t);        while (t--) {scanf ("%i64d%i64d%i64d%i64d", &x,&m,&k,&c);        k*=9;        M%=euler (k);        Ans= (Quick_mod (10,m,k) +k-1)%k;        ans/=9;        k/=9;        Ans= (ans*x)%k;        printf ("%i64d\n", ans);        printf ("Case #%d:\n", p++);        if (ans==c) puts ("Yes");    Else puts ("No"); } return 0;}

problem 1005 BD String

accept:0 submit:0

Time limit:2000/1000 MSec (java/others) Memory limit:65536 KB

Problem Description

As we all know, the degree bears like the characters only two: B and D.

Today, it invented a rule that consists of a string of B and D:

S (1) =b

S (2) =BBD

S (3) =BBDBBDD

...

S (n) =s (n-1) +b+reverse (Flip (S (n-1))

where reverse (s) refers to flipping a string, such as reverse (BBD) =dbb,flip (s), refers to replacing B in a string with a d,d replaced with B, such as Flip (BBD) =ddb.

Although the degree bears usually only use its computer to play again and again, this does not hinder the machine unparalleled computing speed, currently it has worked out the contents of S (), but the degree of the bear is only a bear, one can not read so long string. It now wants to know the first bit of the string (starting from 1) to the R bit, what is the number of B contained?

Input first line an integer t representing the T (1≤t≤1000) group data. Each set of data contains two numbers L and R (1≤l≤r≤).

Output

For each set of data, the output s () represents the number of bits of the string L to B in the R bit.

Sample INPUT3
1 3
1 7
4 8 Sample Output2
4
3

Problem idea

Problem-Solving ideas: The following points can be seen from the string rules

The string length of ①s (n) is;

②s (n) The number of B is a (centered in the middle of the B, assuming that there is x B on the left, then there is a right, that is, the number of the left B + the number of the right B =s (n-1) the string length)

③ is so much bigger than that, it just scares us.

From the above points, the problem is not as scary as it seems.

request interval [l,r] The number of B, we can be converted to seek [1,R]-[1,L-1]

And the number of B in the interval [1,x], and can be as described in section ②, split into 3 paragraphs, recursive solution

such as x=12, S=BBDBBDDBBBDD ...

Split into 3 paragraphs as follows:

The 4 on the right is superfluous, but because the right side is obtained by Reverse+flip, the number of the 4 B in the right can be converted to 4-([1,7] number of B in-[1,3] b)

and the number in [1,3] is solved by recursion.

This topic recursion to speak more around, if the small partner did not understand, can leave a comment, I will reply in time

Topic link →HDU 5694 BD String

Source Code

/*Sherlock and Watson and adler*/#pragma comment (linker, "/stack:1024000000,1024000000") #include <stdio.h> #include < string.h> #include <stdlib.h> #include <queue> #include <stack> #include <math.h> #include <vector> #include <map> #include <set> #include <cmath> #include <complex> #include < string> #include <algorithm> #include <iostream> #define EXP 1e-10using namespace Std;const int N = 100005;    const int M = 40;const int inf = 100000000;const int mod = 2009;__int64 dfs (__int64 x) {if (!x) return 0;    __int64 M=0,ans,res;    while (m<x) m=m*2+1;    M= (m-1)/2;    ans= (m+1)/2;    Res=x-m-1; Return ans+1+res-(Ans-dfs (M-res));}    int main () {int t;    __int64 L,r;    scanf ("%d", &t);        while (t--) {scanf ("%i64d%i64d", &l,&r);    printf ("%i64d\n", DFS (R)-dfs (L-1)); } return 0;} 

problem 1006 Gym Class

accept:0 submit:0

Time limit:6000/1000 MSec (java/others) Memory limit:65536 KB

Problem Description

As we all know, the degree bears like all kinds of sports activities.

Today, it finally became the dream of the physical education teacher. For the first time in class, it found an interesting thing. Before class, all the classmates to be lined up, assuming that at first everyone has a unique ID, from 1 to N, after the row, each student will find the minimum ID of all the students in front of them, including themselves, as a score of their own evaluation of this class. Trouble is, some students do not want to some (some) classmates in front of him (her), in the case of satisfying this premise, the new PE teacher-degree bear, hope that the final line results can make all students evaluation scores and maximum.

Input

The first line is an integer t, which represents the T (1≤t≤30) group data. For each set of data, the first line enters two integers N and M (1≤n≤100000,0≤m≤100000), each representing the total number of people and the preferences of some students. The next m line, two integers a and B (1≤a,b≤n) per line, indicates that the classmate with ID A does not want the classmate with ID B to rank before him or her. You can assume that the title guarantee has at least one arrangement that meets all requirements.

Output

For each set of data, the maximum score is output.

Sample INPUT3
1 0
2 1
1 2
3 1
3 1 Sample OUTPUT1
2
6 Problem idea

Problem-solving ideas: It is well known that in order to make everyone's rating as high as possible, in the absence of conditions, must be high-ID row in front of the best

But the bears are not so kind.

The classmate with ID B can't be in front of a, OK, how to do it?

For this scheduling problem, we might as well use topological sorting to solve

Assuming b can't be in front of a, then I'm going to draw an edge from A to B between AB, so we can get a directed graph

We want to go from the big to the small row by the ID, but also consider whether the current classmate has special request

Above the map, where the degree of 0 of the node, indicating that no classmates do not want the classmate in front of it, so the classmate can follow the ID from the big to the small row

Therefore, the practice is that each time the 0 of the node into the priority queue, select the highest ID node first row, and then the ID of the classmates hate all the students into the degree-1, if the degree into 0 also to be placed in the priority queue to determine the ID successively, where the map can be implemented adjacency table, you can also use some vector

Topic link →HDU 5695 Gym Class

Source Code

/*sherlock and Watson and adler*/#pragma comment (linker, "/stack:1024000000,1024000000") #include <stdio.h># include<string.h> #include <stdlib.h> #include <queue> #include <stack> #include <math.h > #include <vector> #include <map> #include <set> #include <cmath> #include <complex># include<string> #include <algorithm> #include <iostream> #define EXP 1e-10using namespace Std;const int N = 100005;const int M = 40;const int inf = 100000000;const int mod = 2009;struct cmp2{bool operator () (int &a , int &b) {return a<b;//max precedence}};int N, m;int indegree[n];p riority_queue<int,vector<int>,cmp2    > q;//Maximum Priority vector<int> V[n];int main () {int t,i,a,b,p,min;    __int64 ans;    scanf ("%d", &t);        while (t--) {ans=0;        Min=inf;        memset (indegree,0,sizeof (Indegree));        for (i=1;i<=n;i++) v[i].clear ();        while (!q.empty ()) Q.pop (); scanf ("%d%d ", &n,&m);            for (i=0;i<m;i++) {scanf ("%d%d", &a,&b);            indegree[b]++;        V[a].push_back (b);        } for (i=1;i<=n;i++) if (!indegree[i]) Q.push (i);            while (!q.empty ()) {p=q.top ();            Q.pop ();            Min=min (p,min);            Ans+=min;                For (I=0;i<v[p].size (); i++) {indegree[v[p][i]]--;            if (!indegree[v[p][i]]) Q.push (V[p][i]);    }} printf ("%i64d\n", ans); } return 0;}
Rookie Growth notes

2016 "Baidu Star"-Preliminary (Astar round2a) Problem Solving report

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.