Loading

牛客多校 第四场 B,H 题解

B签到成功

 

 打表出c的存在没什么作用。

容易找出规律 答案就是c的某个次数,这个次数就是n的质因子的幂次和。

因此只需线性筛出幂次和,再快速幂就好了

#pragma warning(disable:4996)

#include<iostream>
#include<algorithm>
#include<bitset>
#include<tuple>
#include<unordered_map>
#include<fstream>
#include<iomanip>
#include<string>
#include<cmath>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<stack>
#include<sstream>
#include<cstdio>
#include<ctime>
#include<cstdlib>
#define INF 0x3f3f3f3f
#define inf 0x7FFFFFFF
#define MOD 1000000007
#define moD 1000000003
#define pii pair<ll,int>
#define eps 1e-7
#define equals(a,b) (fabs(a-b)<eps)
#define bug puts("bug")
#define re  register
#define fi first
#define se second
#define pb push_back
const int maxn = 1e6 + 5;
const double Inf = 10000.0;
const double PI = acos(-1.0);
typedef  long long ll;
typedef unsigned long long ull;
using namespace std;


ll num[maxn];   
int vis[maxn];     
int num_sieve(int n) {
    int cnt = 0;
    for (int i = 2; i <= n; i++) {
        if (!vis[i]) num[i] = 1, vis[i] = 1;
        int j = 2;
        while (j * i <= maxn) if (vis[j]) num[i * j] = num[i] + num[j],vis[j * i] = 1,j++; else break;
    }
    return cnt;  //返回x小于等于n的s素数的个数
}




ll quickPower(ll a, ll b, ll m) {  
    ll ans = 1;
    ll base = a;
    while (b) {
        if (b & 1) {
            ans *= base;
            ans %= m;
        }
        base *= base;
        base %= m;
        b >>= 1;   
    }
    return ans;
}



int main() {
    int T;
    num_sieve(maxn - 3);
    ll n, c;
    scanf("%d", &T);
    while (T--) {
        scanf("%lld%lld", &n, &c);
        if (n == 1ll) {
            puts("1"); continue;
        }
        ll cnt = 0;
       // cout << cnt << endl;
        printf("%lld\n", quickPower(c, num[n] , MOD));
    }

}
View Code

 

H 。

给出n,要求从n种分出两组a,b,使ab两两满足gcd > 1

1.显然1和对于p * 2 > n 的p不可能是答案。

2.对每个数取最大质因子成为一个集合。

若集合个数是偶数,可以直接匹配。

否则,把2p扔进另一组数,剩余的可以两两匹配。

#pragma warning(disable:4996)

#include<iostream>
#include<algorithm>
#include<bitset>
#include<tuple>
#include<unordered_map>
#include<fstream>
#include<iomanip>
#include<string>
#include<cmath>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<stack>
#include<sstream>
#include<cstdio>
#include<ctime>
#include<cstdlib>
#define INF 0x3f3f3f3f
#define inf 0x7FFFFFFF
#define MOD 1000000007
#define moD 1000000003
#define pii pair<int,int>
#define eps 1e-7
#define equals(a,b) (fabs(a-b)<eps)
#define bug puts("bug")
#define re  register
#define fi first
#define se second
#define pb push_back
const int maxn = 5e5 + 5;
const double Inf = 10000.0;
const double PI = acos(-1.0);
typedef  long long ll;
typedef unsigned long long ull;
using namespace std;

int prime[maxn];
int is_prime[maxn];
int cnt;
int pfac[maxn];

void is_Prime() {
    is_prime[0] = is_prime[1] = 1;
    for (int i = 2; i <= maxn -3; i++) {
        if (!is_prime[i]) {
            prime[cnt++] = i;
            pfac[i] = i;
            for (int j = 2 * i; j <= maxn - 3; j += i) {
                is_prime[j] = 1;
                pfac[j] = i;
            }
        }
    }
}

vector<int> p[maxn];

int main() {
    int T;
    int m;
    is_Prime();
    scanf("%d", &T);
    while (T--) {
        memset(p, 0, sizeof p);
        scanf("%d", &m);
        vector<pii> ans;
        vector<int> tmp;
        for (int i = 2; i <= m; i++) {
            if (!is_prime[i] && i * 2 > m) continue;
            p[pfac[i]].push_back(i);
        }
        for (int i = 0;; i++) {
            if (prime[i] > m) break;
            if (p[prime[i]].empty()) continue;
            if (p[prime[i]].size() == 1) continue;
            if (p[prime[i]].size() & 1) {
                tmp.push_back(p[prime[i]][1]);
                ans.emplace_back(p[prime[i]][0], p[prime[i]][2]);
                for (int k = 3; k + 1< p[prime[i]].size(); k += 2) {
                    ans.emplace_back(p[prime[i]][k], p[prime[i]][k + 1]);
                }
            }
            else {
                for (int k = 0; k + 1 < p[prime[i]].size(); k += 2) ans.emplace_back(p[prime[i]][k], p[prime[i]][k + 1]);
            }
        }
        for (int i = 0; i + 1 < tmp.size(); i+=2) {
            ans.emplace_back(tmp[i], tmp[i + 1]);
        }
        printf("%d\n", ans.size());
        for (auto it : ans) {
            printf("%d %d\n", it.fi, it.se);
        }
    }
}
View Code

 

posted @ 2020-07-20 19:25  MQFLLY  阅读(181)  评论(0编辑  收藏  举报