SGU 代码集 (Volume 1)

SGU 100~119 一句话题解,代码 (更新中……)

100 A+B problem 

101 Domio 构图,求一个欧拉路

# include <iostream>
# include <cstdio>
# include <cstring>
# include <vector>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define PNA {puts("No solution");return 0;}
#define N 10
#define M 210
int d[N], c[N][N], ans[M], tot;
vector<int> g[N][N];

void add_edge(int x,int y,int c)
{
    g[x][y].push_back(c);
}

inline int abs(int x)
{
    return x<0? -x:x;
}

void Dfs(int x)
{
    for(int i=0; i<7; i++)
        if(c[x][i])
        {
            c[x][i]--; c[i][x]--; Dfs(i);
        }
    ans[tot++]=x;
}

void write(int x,int y)
{
    if(g[x][y].size())
    {
        printf("%d +\n",g[x][y].back());
        g[x][y].pop_back();
    }else
    {
        printf("%d -\n",g[y][x].back());
        g[y][x].pop_back();
    }
}

int main()
{
    int m,x,y; int S;
    scanf("%d",&m);
    rep(i,m){
        scanf("%d%d",&x,&y);
        g[x][y].push_back(i+1);
        c[x][y]++; c[y][x]++;
        d[x]++; d[y]++;
    }
    tot = 0;
    rep(i,7) if(d[i])S = i;
    rep(i,7) if(d[i]&1) S = i, tot++;
    if (tot>2) PNA;
    tot = 0; Dfs(S);
    if(tot<=m) PNA;
    rep(i,m) write(ans[i],ans[i+1]);
    //while(1);
    return 0;
}

102 Coprimes 求phi(n)

# include <iostream>
# include <cstring>
using namespace std;

bool p[10001];

void prime_make(int N)
{
    memset(p,1,sizeof(p));
    for(int i=2; i*i<=N; i++) if(p[i])
        for(int j=i<<1; j<=N; j+=i)
            p[j] = false;
}

void solve(const int n)
{
    int res = n;
    for(int i=2; i<=n; i++)
        if(p[i] && n%i==0)
            res -= res/i;
    cout << res << endl;
}

int main()
{
    int n;
    cin >> n;
    prime_make(n);
    solve(n);
}

103 ...
104 ...

105 Div 3 水题

# include <iostream>
int main()
{
    long long n;
    std::cin >> n;
    std::cout << n - (n+2)/3;
}

106 ...

107 987654321problem 水题


# include <iostream>
# include <cstdio>
using namespace std;
const int m =987654321;
typedef long long ll;
bool cmp(ll a,ll b,int len)
{
    while(len--)
    {
        if(a%10!=b%10)
            return false;
        a/=10; b/=10;
    }
    return true;
}

long long pow = 1;
int t;
void dfs(ll x,int dep)
{
    if(dep==9)
    {
        //cout << x << endl;
        t++;
        return;
    }
    long long cur = x;
    for(int i=0; i<10; i++)
    {
        if(cmp(cur*cur,m,dep+1))
        {
            pow *=10;
            dfs(cur,dep+1);
            pow /=10;
        }
        cur += pow;
    }
}

int main()
{
    dfs(0,0);
    int n;
    cin >> n;
    if(n<=8)
        cout << 0 << endl;
    else if(n==9)
        cout << t << endl;
    else {
        cout << t*9;
        int k = n-10;
        while(k--)printf("0");
        cout<<endl;
    }
    return 0;
}

108 ~ 119 ……


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值