kuangbin topic number theory foundation part1?

Segment tree themes too hard, then I do the math right!

But mathematics too difficult, I ...... (pull

 

These days want to do a check put together a few math.

In addition to some advanced knowledge, like Mobius inversion, Du teach sieve, min25 not learn I jumped, some basic thinking can still record it.

 

ex_gcd 

POJ 1061 Frog dating POJ 2115 C Looooops

SGU 106 The equation

Trifecta.

Talk to understand it, I did not understand the principle (pull

That is, by seeking out exgcd gcd (a, b) = d, and c% d! = 0 None solution.

Then a, b, c d, respectively, divided by the number conventions. a / = d, b / = d, c / = d;

exgcd out x = 1, x * = c is a particular solution. Wherein x is the smallest positive solution (here including 0) is (x% b + b)% b; y may be appropriate in this case with y = (ca * x) / b in turn solved for.

Similarly y smallest positive solution to obtain respective, y * = c then (y% a + a)% a;

Here we must pay special attention to try to make a, b are initially positive, but also pay special attention to ensure that the form of the equation ax + by = c of such, in particular, the position of c, c negative does not matter.

General solution can be any number of units by adding Laid solution, such as x = x0 + b * k; y = y0-a * k;   

What solution system? I line represents Wangla (cry

 

Note that if each SGU 106 and a negative parameter, k corresponding to the calculated range to swap out. Because you see the minus sign in the denominator Yeah.

We have to use ceil and floor. It has been a precision error, I do not want to use this function, later succumbed.

 

 

UVA 11752 The Super Powers

Yeah I thought this problem, although I fork. Hand to play it and found index is a composite number can be the answer.

Then I began to think with the priority queue! Close to the prime power several times to join the answer. Then gone, WA for a while.

In fact, the number of bonding together several power there answer ah, such as 6 ^ 4, can mess 6 ^ (2 * 2 || 1 * 4) and this is a no-show in front of! So, ah answer to deal with all the numbers. With set maintenance, you can not use the priority queue.

Sentenced to overflow details

That I did not think ah, entitled ull, always wanted to be judged according to the number of negative or smaller, it is downright.

Or read the solution to a problem to know, with the upper limit ull each time in addition to this number, each number up to the pre-power index, the index power up to less than 4 (the smallest composite number), and then break off.

Do not iron head sentenced to overflow ah, in turn handle up to a few index, which is very spiritual right. (I do not have...

 

LightOJ L370 Bi-Shu and fi-Shu

POJ 2478 Farey sequence

Euler two screens, progress is to me a few months of my screen template upgrade from linear to log the booming trance. Fortunately linear understanding, but min25 and Du will teach impossible to understand the current 555.

 

UVA 11426 GCD - Extreme (II)

This classic ah feel good question. I can think of pretreatment Euler function, to find for each factor i n that root n ^ 3/2 gone ah.

 

Then they discovered a new thinking, to get factor for each number, a process is not easy to find, but you go to count on the contribution of each factor have on his other numbers are very good out of!

Further a gcd formula I find for each n x and n satisfy gcd is the number of i, gcd (n, x) = i, gcd (n / i, x / i) = 1; this is not phi [n / i]! The contribution of each factor is converted to i * phi [n / i]

Then there was added to the contribution factor to 1,2,3,4,5 ...... n + n / 2 + n / 3 + n / 4 + n / 5 + ..... + 1+ .... + 0 count the total complexity ye ah, md I actually do not, anyway, than the above outstanding

 

Also, I like either a comment in the same statistical sum [i], O (1) answer, such as pre-processing complexity to the above formula; about 500ms;

Also found others seeking a prefix and then direct statistics for each number O (n) of the Euler function, the need to understand it. With only about 200ms;

 

 1 #include <bits/stdc++.h>
 2 #define debug(x) cout << #x << ": " << x << endl
 3 using namespace std;
 4 typedef long long ll;
 5 const int INF=0x3f3f3f3f;
 6 const int MOD=1e9+7;
 7 
 8 
 9 const int MAXN=4e6+7;
10 
11 bool notprime[MAXN];
12 //ll sum[MAXN];
13 //ll b[MAXN];
14 ll prime[MAXN/10];
15 ll phi[MAXN];
16 int cnt;
17 
18 void getprime_phi()
19 {
20     cnt = 0;
21     phi[1] = 1;
22     notprime[0]=notprime[1]=1;
23     for(int i = 2;i < MAXN;i++)
24     {
25         if(!notprime[i])
26         {
27             prime[cnt++] = i;
28             phi[i] = i-1;
29         }
30         for(int j = 0;j < cnt && i * prime[j] < MAXN;j++)
31         {
32             notprime[i*prime[j]] = 1;
33             if(i%prime[j]==0)
34             {
35                 phi[i*prime[j]] = phi[i]*prime[j];
36                 break;
37             }
38             phi[i*prime[j]] = phi[i]*(prime[j]-1);
39         }
40     }
41     for(int i=2;i<MAXN;++i) phi[i]+=phi[i-1];
42 }
43 
44 int main()
45 {
46     getprime_phi();
47     /*for(int i=1;i<MAXN;++i)
48     {
49         for(int j=i*2;j<MAXN;j+=i)
50         {
51             b[j]+=1ll*phi[j/i]*i;
52         }
53     }
54 
55     for(int i=2;i<MAXN;++i)
56         sum[i]=sum[i-1]+b[i];*/
57     int n;
58     while(~scanf("%d",&n))
59     {
60         if(!n) break;
61        // printf("%lld\n",sum[n]);
62         ll ans=0;
63         for(int i=1;i<=n;++i) ans+=1ll*(phi[n/i]-1)*i;
64         printf("%lld\n",ans);
65     }
66     return 0;
67 }
View Code

 

POJ 2116 Death to Binary?

Simulation title

The string value calculation value and converted into a string of packaging function.

Or string easy to use, for string unassigned and can not be accessed by subscript, but you can use + =, directly behind the stick, stick thief, I started with a char array, so hard to do ah.

Then I did not notice this problem input can be 0, did not test, then the emergence of output limit exceed! See live long. And then find out that they wrote false conversion, no special sentenced to 0. Thieves will output more than 0, gone, but not actually WA.

 

#include <string>
#include <iostream>
#include <algorithm>
#include <cstring>

#define debug(x) cout << #x << ": " << x << endl
using namespace std;
typedef long long ll;
const int MAXN=100;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;

ll f[60];
void init()
{
    f[0]=1;f[1]=2;
    for(int i=2;i<50;++i) f[i]=f[i-2]+f[i-1];
}

string s,t,ss,tt,goa;

ll cal(string &str)
{
    ll res=0;
    int len=str.size();
    for(int i=0;i<len;++i)
    {
        if(str[i]=='1') res+=f[len-i-1];
    }
    return res;
}

void standard(string &tmp, ll val)
{
    tmp="";
    for(int i=40;i>=0;--i)
    {
        if(val>=f[i])
        {
            val-=f[i];
            tmp+='1';
        }
        else if(tmp!="")
        {
            tmp+='0';
        }
    }
    if(tmp=="") tmp="0";
}

int main()
{
    init();
    while(cin>>s>>t)
    {
        ll anss=cal(s);
        standard(ss,anss);

        ll anst=cal(t);
        standard(tt,anst);

        ll ans=anss+anst;
        //debug(ans);
        standard(goa,ans);
        int len=goa.size();
        int lens=ss.size();
        int lent=tt.size();

        for(int i=0;i<len+2-lens;++i) cout<<' ';
        cout<<ss<<'\n';
        cout<<"+ ";
        for(int i=0;i<len-lent;++i) cout<<' ';
        cout<<tt<<'\n';
        cout<<"  ";
        for(int i=0;i<len;++i) cout<<'-';
        cout<<'\n';
        cout<<"  "<<goa<<'\n'<<'\n';
    }
    return 0;
}

#include <string>
#include <iostream>
#include <algorithm>
#include <cstring>

#define debug(x) cout << #x << ": " << x << endl
using namespace std;
typedef long long ll;
const int MAXN=100;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;

ll f[60];
void init()
{
    f[0]=1;f[1]=2;
    for(int i=2;i<50;++i) f[i]=f[i-2]+f[i-1];
}

string s,t,ss,tt,goa;

ll cal(string &str)
{
    ll res=0;
    int len=str.size();
    for(int i=0;i<len;++i)
    {
        if(str[i]=='1') res+=f[len-i-1];
    }
    return res;
}

void standard(string &tmp, ll val)
{
    tmp="";
    for(int i=40;i>=0;--i)
    {
        if(val>=f[i])
        {
            val-=f[i];
            tmp+='1';
        }
        else if(tmp!="")
        {
            tmp+='0';
        }
    }
    if(tmp=="") tmp="0";
}

int main()
{
    init();
    while(cin>>s>>t)
    {
        ll anss=cal(s);
        standard(ss,anss);

        ll anst=cal(t);
        standard(tt,anst);

        ll ans=anss+anst;
        //debug(ans);
        standard(goa,ans);
        int len=goa.size();
        int lens=ss.size();
        int lent=tt.size();

        for(int i=0;i<len+2-lens;++i) cout<<' ';
        cout<<ss<<'\n';
        cout<<"+ ";
        for(int i=0;i<len-lent;++i) cout<<' ';
        cout<<tt<<'\n';
        cout<<"  ";
        for(int i=0;i<len;++i) cout<<'-';
        cout<<'\n';
        cout<<"  "<<goa<<'\n'<<'\n';
    }
    return 0;
}
View Code

 

 

to sum up:

In addition to the template and then Euler title screen, the other seems to have more or less checked a solution to a problem.

But still adhere to think that he realized, WA T before going to the investigation.

But lamented that her or TCL, but also to refuel ah. Algorithm competition has always been a need to experience it!

Guess you like

Origin www.cnblogs.com/Zzqf/p/12074590.html