B.Basic Gcd Problem

www我的锅,一直以为1返回的是c没有特判,直到队友问了才发现QAQ

就是简单的快数幂

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi acos(-1)
const int maxn=1e6+10;
const int mod=1e9+7;
ll ksm(ll x,int n)
{
    ll ans=1;
    while(n)
    {
        if(n%2==1)
            ans*=x,ans%=mod;
        x*=x;
        x%=mod;
        n/=2;
    }
 
    return ans;
}
int f[maxn];
int b[maxn],s;
int main()
{
    int n,c,t,x;
    f[1]=1;
    s=1;
    b[1]=1;
    for(int i=2;i<=1000;i++)
    {
        if(f[i]==0)
        {
            s++;
            b[s]=i;
            for(int j=i+i;j<=1000000;j+=i)
                f[j]=1;
        }
    }
    cin>>t;
    while(t--)
    {
        scanf("%d %d",&n,&c);
        if(n==1)
        {
            printf("1\n");
            continue;
        }
        int ans=0;
        if(f[n]==0)
            ans=1;
        else
        {
            ans=0;
            for(int i=2;i<=s;i++)
            {
                while(n%b[i]==0)
                {
                    ans++;
                    n/=b[i];
                }
                if(n==1)
                break;
                if(f[n]==0)
                {
                    ans++;
                    break;
                }
 
            }
         }
        ll p=ksm(c,ans);
        printf("%lld\n",p);
    }
    return 0;
}

 

F.Finding the Order

签到,多种情况讨论一下,队友A的

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define sc(a) scanf("%d",&a);
#define pf printf
#define pi acos(-1)
const int mod=1e9+7;
const int N=2e6+10;
int n,m;
 
int main()
{
    int t;
    sc(t);
    while(t--)
    {
        int a,b,c,d;
        cin>>a>>b>>c>>d;
        int f;
        if(a>c){
            if(b>d&&d>c)f=1;
            else f=0;
        }
        else if(a<c){
            if(b<d&&c<d)f=0;
            else f=1;
        }
        else {
            if(b<d)f=0;
            else f=1;
        }
        if(f)puts("AB//CD");
        else puts("AB//DC");
    }
    return 0;
}

H.Harder Gcd Problem

一开始想了很多假算法,打出写法后又因为机子太差跑不出200000的结果以为自己复杂度算错了,怀疑了好久,最后决定莽一发竟然过了

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi acos(-1)
const int maxn=1e6+10;
const int mod=1e9+7;
int f[maxn];
vector<ll>q[10001];
int vis[maxn],ans,s,u[maxn];
int a[maxn],b[maxn],v[maxn];
int main()
{
    s=0;
    for(int i=2;i<=1000;i++)
    {
        if(f[i]==0)
        {
            for(int j=i+i;j<=200000;j+=i)
            {
                f[j]=1;
            }
        }
    }
    for(int i=2;i<=100000;i++)
    {
        if(f[i]==0)
        {
            s++;
            u[s]=i;
        }
    }
    for(int i=1;i<=s;i++)
    {
        q[i].push_back(u[i]);
        v[u[i]]++;
        for(int j=u[i]*2;j<=200000;j+=u[i])
        {
            q[i].push_back(j);
            v[j]++;
        }
 
    }
    int t,n,m;
    cin>>t;
    while(t--)
    {
        ans=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)vis[i]=0;
        int l=upper_bound(u+1,u+1+s,(n+1)/2)-u;
        for(int i=l-1;i>=1;i--)
        {
            int len;
            len=q[i].size();
            if(n<q[i][len-1])
            len=upper_bound(q[i].begin(),q[i].end(),n)-q[i].begin();
            int sum=0,max1=0,op=0;
            for(int j=0;j<len;j++)
            {
                if(vis[q[i][j]]==0)
                {
                    sum++;
                    if(max1<v[q[i][j]])
                    {
                        max1=v[q[i][j]];
                        op=j;
                    }
                }
            }
            if(sum%2==0)
            {
                int l=0,r;
                while(1)
                {
                    while(vis[q[i][l]]&&l<len)
                    {
                        l++;
                    }
                    if(l>=len)
                        break;
                    r=l+1;
                    while(vis[q[i][r]]&&r<len)
                    {
                        r++;
                    }
                    if(r>=len)
                        break;
                    ans++;
                    vis[q[i][r]]=1;
                    vis[q[i][l]]=1;
                    a[ans]=q[i][l];
                    b[ans]=q[i][r];
                    l=r+1;
                }
            }
            else
            {
                int l=0,r;
                while(1)
                {
                    if(l==op)
                        l++;
                    while(vis[q[i][l]]&&l<len)
                    {
                        l++;
                        if(l==op)
                            l++;
                    }
                    if(l>=len)
                        break;
                    r=l+1;
                    if(r==op)
                        r++;
                    while(vis[q[i][r]]&&r<len)
                    {
                        r++;
                        if(r==op)
                            r++;
                    }
                    if(r>=len)
                        break;
                    ans++;
                    vis[q[i][r]]=1;
                    vis[q[i][l]]=1;
                    a[ans]=q[i][l];
                    b[ans]=q[i][r];
                    l=r+1;
                }
            }
        }
        cout<<ans<<endl;
        for(int i=1;i<=ans;i++)
        {
            printf("%d %d\n",a[i],b[i]);
        }
    }
    return 0;
}