Changle National training Day1

T1 statistics

topic

Description [title]

Set S (N) represents the sum of digits of N, such as S (484) = 4 + 8 + 4 = 16, S (22) = 2 + 2 = 4.

If a positive integer satisfying S (x * x) = S (x) * S (x), we call Rabbit N umber. For example, 22 is a Rabbit N umber, as S (484) = S (22) * S (22).

Now, given an interval [L, R], the number of seek Rabbit N umber within the interval.

[Input Format]

Input line only, two spaces separated by the number of L and R.

[Output format]

Output line only an integer representing the number of Rabbit N umber asked for.

[Sample input]

58 484

[Sample Output]

24 

[Data] scale

1 <= L <= R <= 10^9

Resolve

Reading a first reaction Title: enumeration from L to R, the number of each turn is determined not Rabbit N umber. However, the data size of 10 9 , apparently timed out.

But all right, so after violence, just try some numbers to see if there is no law.

~. 1 the Rabbit N Umber in 1000 as follows:

1 2 3 10 11 12 13 20 21 22 30 31 100 101 102 103 110 111 112 113 120 121 122 130 200 201 202 210 211 212 220 221 300 301 310 311 1000

Not difficult to find, no matter on which one, no number greater than 3 (also, you can see for yourself a larger scale), as to why, it will not be given here in detail proved ( because this is not konjac ).

So there will be some paper-cut: there is a number greater than 3, skip any position.

So there are two ways:

  1. dfs + paper-cut
  2. Half play table +

Play table here is not to say ( too much trouble ), speaking here dfs practices:

dfs (int temp): temp can be understood as the current number is the number of temp + a new one digits, specifically look at the code to understand.

Temp = 0 from the start to search, in each processing function dfs number of bits is 0123, and the condition in the range of L ~ R on the cumulative number,

After the process, if the number is less than equal to R / 10, then it dfs (x) (i.e., number of bits can be increased further).

Finally, like the total number of output, do not forget to open long long, as for S (x), like a direct analog.

Code

#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
using namespace std;
long long read()
{
    long long num=0,w=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-') w=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        num=(num<<1)+(num<<3)+ch-'0';
        ch=getchar();
    }
    return num*w;
}
long long l,r,ans;
int S(long long x)
{
    int n=0;
    while(x>0)
    {
        n+=x%10;
        x/=10;    
    }
    return n;
}
void dfs(int temp)
{
    for(int i=0;i<=3;i++)
    {
        long long x=temp*10+i;
        int s=S(x);
        if(x==0||S(x*x)!=s*s) continue;
        if(l<=x&&r>=x) ans++;
        if(x<=r/10) dfs(x);
    }
}
int main()
{
    //freopen("rabbit.in","r",stdin);
    //freopen("rabbit.out","w",stdout);
    l=read(),r=read();
    dfs(0);
    cout<<ans;
    return 0;
}
View Code

 

 

 

 

 

T2 program number of edges

topic

Description [title]

You have no n to a point m of edges connected graph, each edge has a right side, provided DISA i represents the midpoint of this figure 1 is the shortest distance to the point i.

Now ask you omitted in this figure, m- (n-1) edges, so that this pattern becomes a tree, provided DISB i represents the midpoint of the tree is a point i the shortest distance. Now you find, how many border erase the program, such that for any i, there DISA i = DISB i .

[Input Format]

The first line contains two positive integers n, m, represents the points and edges connected undirected graph.

Then there are m rows, each row having three positive integers u, v, w, expressed between the points u and w v is a right side edge of the undirected.

Side weight to ensure that no data, no self-loop.

[Output format]

Output line an integer number of programs satisfying the condition represented by the result of modulo 2147483647.

[Sample input]

3 3
1 2 2
1 3 1
2 3 1

[Sample Output]

2 

[Data] scale

Resolve

 It is said that a man named shortest FIG things, is to satisfy the original dis (u) + w = ​​dis (v) the edge (u, v, w) retained subgraph configuration.

In this problem, the right side must be a positive integer, so the shortest path graph is a directed acyclic graph, the answer is simply enumeration has directed acyclic graph spanning tree to the number, but still could not pass.

In fact, the shortest construction diagram of the process, that is, for each click on a father, while the optional point total is the father of this, obviously the answer is the product of the degrees.

Specific implementation is the shortest, is used in the present Tacca Dijkstra.

Code

#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
using namespace std;
priority_queue<pair<int,int> > q;
const int N=1010,M=1000100;
const long long mod=2147483647;
int n,m,head[N],ver[M],edge[M],from[M],tot,next[M],d[N],deg[N];
long long ans=1;
bool v[N];
void add(int x,int y,int z)
{
    ver[++tot]=y,edge[tot]=z,from[tot]=x,next[tot]=head[x],head[x]=tot;
}
void dijkstra()
{
    memset(d,0x7f7f7f7f,sizeof(d));
    memset(v,false,sizeof(v));
    d[1]=0;
    q.push(make_pair(0,1));
    while(q.size())
    {
        int x=q.top().second;
        q.pop();
        if(v[x]) continue;
        v[x]=1;
        for(int i=head[x];i;i=next[i])
        {
            int y=ver[i],z=edge[i];
            if(d[y]>d[x]+z)
            {
                d[y]=d[x]+z;
                q.push(make_pair(-d[y],y));
            }
        }
    }
    for(int i=1;i<=tot;i++)
    {
        int x=from[i],y=ver[i],z=edge[i];
        if(d[x]+z==d[y]) deg[y]++;
    }
    for(int i=1;i<=n;i++)
        if(deg[i]) ans=(1LL*ans*deg[i])%mod;
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int x,y,z;
        cin>>x>>y>>z;
        add(x,y,z);
        add(y,x,z);
    } 
    Dijkstra (); 
    cout << years;
    return  0 ; 
}
View Code

 

 

 

 

 

T3 square root

topic

Description [title]

[Input Format]

Comprising a plurality of sets of input data. Each row of data includes two positive integers L, R.

File (not require an output end) 00 at the end.

[Output format]

For each test, the output line represents the answer. Ensure answer 0,2 [ 63 within range).

[Sample input]

2 10
248832 248832
0 0

[Sample output]

13
5

[Data] scale

Resolve

Frightening number of topics ~~~

And first summation interval represented by the prefix, so only needs [1, n] answers.

Found sequentially each number f (i) cumbersome, considering the number of request [1, n] of the f (i) = k, which number is represented by g (k).

Represents a [1, n] k number of values or a positive integer power of opening can be obtained p (k) = n with P (k) . 1 / k (rounded down).

Then .

So with the introduction of recursive answer it.

Code

#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
using namespace std;
const double eps=1e-10;
long long l,r,f[100];
long long solve(long long x)
{
    if(x<2) return 1;
    int num=0;
    for(int i=63;i>=2;i--)
    {
        f[i]=(long long)(pow(x,(double)1.0/i)+eps)-1;
        for(int j=i+i;j<=63;j+=i) f[i]-=f[j];
        num+=f[i]*(i-1); 
    }
    return num+x;
}
int main()
{
    cin>>l>>r;
    while(l!=0&&r!=0)
    {
        cout<<solve(r)-solve(l-1)<<endl;
        cin>>l>>r;
    }
    return 0;
}
View Code

 

 

 

 

 

T4 travel

topic

Description [title]

[Input Format]

The first line contains two non-negative integer n, k, meaning as the [title] Description.

Next, n-1 lines, each line three positive integers u, v, w, represents u, v have an edge between the right side as w.

[Output format]

Output line An integer that represents the answer. To ensure that there is a legitimate solution.

[Sample input]

5 6
1 2 3
1 3 4
2 4 2
2 5 3

[Sample Output]

4 

[Data] scale

Resolve

Easy to find:

  • dis(u,v)=dis(u,1)+dis(v,1)。
  • If the number of passes is an odd number side, then there must be a deep depth odd point, another point is an even number.

Therefore, according to the parity of all points to classify depth, so there two sequences a, b, only need A I + B I of smaller value to the k.

Code

#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
using namespace std;
const int N=100010;
struct node
{
    long long v;
    int p;
    node(long long a,int b):v(a),p(b){}
    bool operator < (const node &a) const
    {
        return v>a.v;
    }
};
priority_queue<node> q;
int head[N],ver[N<<1],edge[N<<1],next[N<<1],tot;
long long dis[N],deep[N],a[N],b[N],cnta,cntb;
void add(int x,int y,int z)
{
    ver[++tot]=y,edge[tot]=z,next[tot]=head[x],head[x]=tot;
}
void dfs(int x,int fa)
{
    for(int i=head[x];i;i=next[i])
    {
        int y=ver[i],z=edge[i];
        if(y==fa) continue;
        dis[y]=-dis[x]+z,deep[y]=deep[x]+1;
        dfs(y,x);
    }
}
int main()
{
    int n,k;
    cin>>n>>k;
    for(int i=1;i<=n-1;i++)
    {
        int x,y,z;
        cin>>x>>y>>z;
        add(x,y,z);
        add(y,x,z);
    }
    dfs(1,0);
    for(int i=1;i<=n;i++)
        if(deep[i]&1) a[++cnta]=dis[i];
        else b[++cntb]=dis[i];
    sort(b+1,b+cntb+1);
    for(int i=1;i<=cnta;i++) q.push(node(a[i]+b[1],i));
    for(int i=1;i<=cnta;i++) head[i]=1;    
    while(k>1)
    {
        node t=q.top();
        q.pop();
        if((++head[t.p])<=cntb) q.push(node(a[t.p]+b[head[t.p]],t.p));
        k--;
    }
    cout<<q.top().v;
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/I-Love-You-520/p/11616040.html