欧拉函数:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
ll n,x;
const int mod=1e9+7;

signed main(){
    cin>>n;
    while (n--){
        unordered_map<ll,ll>has;
        cin>>x;
        int g=x;
        for (int i = 2; i <=x/i ; ++i) {
            while (x%i==0){
                has[i]++;
                x/=i;
            }
        }
        if(x>1)
            has[x]++;
        int ans=g;
        for (auto i:has) {
            ans=ans*(i.first-1)/i.first;
        }
        cout<<ans<<endl;
    }
//    ll cnt=1;
//    for( auto p : has ){
//        cnt =cnt * (p.second+1)%mod;
//    }
//    cout<<cnt;
}

筛法求欧拉函数:求1到n 的欧拉函数的和

diamond:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
const int N=1e6+10;
int prime[N],vis[N],cnt;
int phi[N];

int get_phi(int n){
    phi[1]=1;
    for (int i = 2; i <=n ; ++i) {
        if(!vis[i]){
            prime[cnt++]=i;
            phi[i]=i-1;
        }
        for (int j = 0; i*prime[j] <=n ; ++j) {
            int m=i*prime[j];
            vis[m]=1;
            if(i%prime[j]==0){
                phi[m]=prime[j]*phi[i];
                break;
            }
            else{
                phi[m]=(prime[j]-1)*phi[i];
            }
        }
    }
    int ans=0;
    for (int i = 1; i<=n; ++i) {
        ans+=phi[i];
    }
    return ans;

}
signed main(){
    int n;
    cin>>n;
    cout<<get_phi(n);
}

矩阵快速幂:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
const int mod=1e9+7;
struct matrix{
    int c[105][105];
    matrix(){ ::memset(c,0,sizeof c);}
}A,res;
int n,k;
matrix operator*(matrix &x,matrix &y){
    matrix t;
    for (int i = 1; i <=n ; ++i) {
        for (int j = 1; j <=n ; ++j) {
            for (int k = 1; k <=n ; ++k) {
                t.c[i][j]=(t.c[i][j]+x.c[i][k]*y.c[k][j])%mod;
            }
        }
    }
    return t;
}
void ksm(int k){
    for (int i = 1; i <=n ; ++i) {
        res.c[i][i]=1;
    }
    while (k){
        if(k&1)res=res*A;
        A=A*A;
        k>>=1;
    }
}

signed main(){
    cin>>n>>k;
    for (int i = 1; i <=n ; ++i) {
        for (int j = 1; j <=n ; ++j) {
            cin>>A.c[i][j];
        }
    }
    ksm(k);
    for (int i = 1; i <=n ; ++i) {
        for (int j = 1; j <=n ; ++j) {
            cout<<res.c[i][j]<<' ';
        }
        cout<<endl;
    }
}

扩展欧几里得

diamond:

#include<bits/stdc++.h>
using namespace std;
#define int long long
//#define int __int128
typedef pair<int,int>PII;
typedef pair<string,int>PSI;
typedef pair<string,string>PSS;
const int N=2e5+5,INF=0x3f3f3f3f,Mod=1e9+7;
const double eps=1e-6;

int exgcd(int a,int b,int &x,int &y){
    if(!b){
        x=1,y=0;
        return a;
    }
    int d=exgcd(b,a%b,y,x);
    y-=a/b*x;
    return d;
}
void solve(){
    int a,b;
    cin>>a>>b;
    int x,y;
    exgcd(a,b,x,y);
    cout<<x<<' '<<y<<'\n';
}
signed main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int t=1;
    //init();
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}
posted on 2023-09-02 15:46  IR101  阅读(5)  评论(0编辑  收藏  举报  来源