1. 程式人生 > >2016CCPC東北賽補題

2016CCPC東北賽補題

can math different sync ice cati push 遇到 tiny

A - Minimum’s Revenge HDU - 5922

There is a graph of n vertices which are indexed from 1 to n. For any pair of different vertices, the weight of the edge between them is the least common multiple of their indexes.

Mr. Frog is wondering about the total weight of the minimum spanning tree. Can you help him?

Input

The first line contains only one integer T (T≤100), which indicates the number of test cases.

For each test case, the first line contains only one integer n (2≤n≤109), indicating the number of vertices.

Output

For each test case, output one line "Case #x:y",where x is the case number (starting from 1) and y is the total weight of the minimum spanning tree.
Sample Input
2
2
3

Sample Output

Case #1: 2
Case #2: 5

題意

給你一棵樹,有n個節點,編號從1~n,每兩個節點之間都有一條邊權值為兩個節點編號的最小公倍數。問最小生成樹的權值和為多少?

分析

所有都和1相連 形成一個花QAQ...

用long long;

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int T;
    long long n;
    int cas = 1;
    cin>>T;
    while(T--)
    {
        cin>>n;
        printf("Case #%d: %lld\n",cas++,(n+2)*(n-1)/2);
    }
    return 0;
}

C - Mr. Frog’s Problem

One day, you, a clever boy, feel bored in your math class, and then fall asleep without your control. In your dream, you meet Mr. Frog, an elder man. He has a problem for you.

He gives you two positive integers A and B, and your task is to find all pairs of integers (C, D), such that A≤C≤B,A≤D≤B and A/B+B/A≤C/D+D/C

Input

first line contains only one integer T (T≤125), which indicates the number of test cases. Each test case contains two integers A and B (1≤A≤B≤1018).

Output

For each test case, first output one line "Case #x:", where x is the case number (starting from 1).

Then in a new line, print an integer s indicating the number of pairs you find.

In each of the following s lines, print a pair of integers C and D. pairs should be sorted by C, and then by D in ascending order.

Sample Input

2
10 10
9 27

Sample Output

Case #1:
1
10 10
Case #2:
2
9 27
27 9

分析

數據這麽大肯定打表找規律啊,具有一定的對稱性(用線性規劃可證明的證明以後補...)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a,b;
int main()
{
    int T;
    cin>>T;
    int cas=1;
    while(T--)
    {
       cin>>a>>b;
       printf("Case #%d:\n",cas++);
       if(a==b)
       {
           cout<<1<<endl;
           cout<<a<<‘ ‘<<b<<endl;
       }
       else
       {
           cout<<2<<endl;
           cout<<a<<‘ ‘<<b<<endl;
           cout<<b<<‘ ‘<<a<<endl;
       }
    }
    return 0;
}

Mr. Frog’s Game

題太長了不貼了 ,連連看

直接莽就行了 數據這麽小
但是 自己因為全局變量覆蓋部分變量... ...
還有CAse 哇了好多發 真難受

#include <bits/stdc++.h>
using namespace std;
const int maxn=33;
int mp[maxn][maxn];
int meo[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};
int n,m;
bool check()
{
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            if(i==j) continue;
            if(mp[i][m]==mp[j][m])
                return true;
        }
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            if(i==j) continue;
            if(mp[i][1]==mp[j][1])
                return true;
        }
    }
    for(int i=1; i<=m; i++)
    {
        for(int j=1; j<=m; j++)
        {
            if(i==j) continue;
            if(mp[n][i]==mp[n][j])
                return true;
        }
    }
    for(int i=1; i<=m; i++)
    {
        for(int j=1; j<=m; j++)
        {
            if(i==j) continue;
            if(mp[1][i]==mp[1][j])
                return true;
        }
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            for(int t=0; t<4; t++)
            {
                int x=i+meo[t][0];
                int y=j+meo[t][1];
                if(mp[x][y]==mp[i][j])
                    return true;
            }
        }
    }
    return false;
}
int main()
{
    int t;
    cin>>t;
    int cas=1;
    while(t--)
    {
       // int n,m;
        cin>>n>>m;
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                cin>>mp[i][j];
            }
        }
        //cas=1;
        //cout<<"Case #"<<cas++<<‘:‘<<‘ ‘;
        printf("Case #%d: ",cas++);
        if(check())
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    }
    return 0;
}

Basic Data Structure

Mr. Frog learned a basic data structure recently, which is called stack.There are some basic operations of stack:

? PUSH x: put x on the top of the stack, x must be 0 or 1.
? POP: throw the element which is on the top of the stack.

Since it is too simple for Mr. Frog, a famous mathematician who can prove "Five points coexist with a circle" easily, he comes up with some exciting operations:

?REVERSE: Just reverse the stack, the bottom element becomes the top element of the stack, and the element just above the bottom element becomes the element just below the top elements... and so on.
?QUERY: Print the value which is obtained with such way: Take the element from top to bottom, then do NAND operation one by one from left to right, i.e. If atop,atop?1,?,a1 is corresponding to the element of the Stack from top to the bottom, value=atop nand atop?1 nand ... nand a1. Note that the Stack will not change after QUERY operation. Specially, if the Stack is empty now,you need to print ” Invalid.”(without quotes).

By the way, NAND is a basic binary operation:

? 0 nand 0 = 1
? 0 nand 1 = 1
? 1 nand 0 = 1
? 1 nand 1 = 0

Because Mr. Frog needs to do some tiny contributions now, you should help him finish this data structure: print the answer to each QUERY, or tell him that is invalid.

Input

The first line contains only one integer T (T≤20), which indicates the number of test cases.

For each test case, the first line contains only one integers N (2≤N≤200000), indicating the number of operations.

In the following N lines, the i-th line contains one of these operations below:

? PUSH x (x must be 0 or 1)
? POP
? REVERSE
? QUERY

It is guaranteed that the current stack will not be empty while doing POP operation.

Output

For each test case, first output one line "Case #x:w, where x is the case number (starting from 1). Then several lines follow, i-th line contains an integer indicating the answer to the i-th QUERY operation. Specially, if the i-th QUERY is invalid, just print " Invalid."(without quotes). (Please see the sample for more details.)
Sample Input
2
8
PUSH 1
QUERY
PUSH 0
REVERSE
QUERY
POP
POP
QUERY
3
PUSH 0
REVERSE
QUERY

Sample Output

Case #1:
1
1
Invalid.
Case #2:
0

Hint

In the first sample: during the first query, the stack contains only one element 1, so the answer is 1. then in the second query, the stack contains 0, l
(from bottom to top), so the answer to the second is also 1. In the third query, there is no element in the stack, so you should output Invalid.

分析

顯而易見的遇到0就會置1
註意特判 0前面沒有數的時候
用優先隊列簡單模擬一下
cur==1的時候當時沒有考慮到應該pop 還是塞進去了個cur-1
cin輸入會超時

#include <bits/stdc++.h>
using namespace std;
deque<int>dq;
//string s;
int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    bool flag=true;
    int T;
    cin>>T;
    int cas=1;
    while(T--)
    {
        dq.clear();
        int n;
        cin>>n;
        cout<<"Case #"<<cas++<<":\n";
       // printf("Case #%d:\n", cas++);
        for(int i=0; i<n; i++)
        {
            string s;
            cin>>s;
            if(s[2]==‘S‘)
            {
                int x;
                cin>>x;
                if(dq.empty())
                    dq.push_back(x);
                else if(x==0)
                {
                    if (flag)
                        dq.push_front(x);
                    else
                        dq.push_back(x);
                }
                else if(x==1)
                {
                    if(flag)
                    {
                        int cur=dq.front();
                        if(cur==0)
                            dq.push_front(x);
                        else
                        {
                            dq.pop_front();
                            cur++;
                            dq.push_front(cur);
                        }
                    }
                    else
                    {
                        int cur=dq.back();
                        if(cur==0)
                            dq.push_back(x);
                        else
                        {
                            dq.pop_back();
                            cur++;
                            dq.push_back(cur);
                        }
                    }

                }
            }
            else if(s[2]==‘P‘)
            {
                if(flag)
                {
                    int cur=dq.front();
                    if(cur==0||cur==1)
                        dq.pop_front();
                    else
                    {
                        dq.pop_front();
                        cur--;
                        dq.push_front(cur);
                    }
                }
                else
                {
                    int cur=dq.back();
                    if(cur==0||cur==1)
                        dq.pop_back();
                    else
                    {
                        dq.pop_back();
                        cur--;
                        dq.push_back(cur);
                    }
                }
            }
                else if(s[2]==‘V‘)
                    flag=!flag;
                else if(s[2]==‘E‘)
                {
                    int cur;
                    if(dq.empty())
                    {
                        cout<<"Invalid."<<endl;
                        continue;
                    }
                    else if(flag)
                        cur=dq.back();
                    else
                        cur=dq.front();
                    if(dq.size() == 1)
                    {
                        if(cur == 0) cout<<0<<endl;
                        else if(cur%2) cout<<1<<endl;
                        else cout<<0<<endl;
                    }
                    else if(dq.size() == 2)
                    {
                        if(cur==0) cout<<1<<endl;
                        else if(cur%2) cout<<1<<endl;
                        else cout<<0<<endl;
                    }
                    else
                    {
                        if(cur==0)cout<<1<<endl;
                        else if(cur%2) cout<<0<<endl;
                        else cout<<1<<endl;
                    }

                }

            }
        }

    return 0;
}

剩下的題我以後一定會補完的

有兩道應該能不出來了
今天是因為要踢球啊喵...

2016CCPC東北賽補題