codeforces——1010

Table of Contents

codeforces----1010

codeforces----1010A

codeforces----1010C

codeforces----1010D


codeforces----1010A

http://codeforces.com/problemset/problem/1010/A

有n个星球,每个星球具有两个属性:飞行燃油比,降落燃油比,问从1号星球经过2~n号星球,回到1号星球所需要的燃油。

暴力做应该需要注意数据大小,非暴力的做法找到规律即可,写一写就能找到的。

#include<iostream>
#include<cstdio>
using namespace std;
int main() {
    double n, t;
    while ( cin >> n >> t ) {
        double ans = t;
        bool vju = true;
        for ( int i = 0; i < 2 * n; i++ ) {
            double temp;
            scanf ( "%lf", &temp );
            if ( temp <= 1 )  vju = false;
            else  ans *= temp / ( temp - 1 );
        }
        if ( vju )  printf ( "%.10lf\n", ans - t );
        else  cout << "-1" << endl;
    }
    return 0;
}

codeforces----1010C

http://codeforces.com/problemset/problem/1010/C

一共有n种面额的纸币(每种有无数张),在m进制下求可能出现的最后一位的数值。

显然暴力查找是不可能的,这个需要运用裴蜀定理。

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

int fGcd ( int a, int b ) {
    if ( a % b == 0 ) return b;
    return fGcd ( b, a % b );
}

int main() {
    int n, m;
    while ( cin >> n >> m ) {
        bool com[100002];
        memset ( com, false, sizeof com );
        int gcd = -1;
        while ( n-- ) {
            int temp;
            scanf ( "%d", &temp );
            temp = temp % m;
            if ( temp != 0 )
                if ( gcd == -1 ) gcd = temp;
                else gcd = fGcd ( temp, gcd );
            else com[0] = true;
        }
        if ( gcd != -1 ) {
            com[gcd] = true;
            for ( int i = ( 2 * gcd ) % m; i != gcd; i = ( i + gcd ) % m )
                com[i] = true;
        }
        int flag = 0;
        for ( int i = 0; i < m; i++ )
            if ( com[i] ) flag++;
        printf ( "%d\n", flag );
        for ( int i = 0; i < m; i++ )
            if ( com[i] ) printf ( "%d ", i );
        cout << endl;
    }
    return 0;
}

codeforces----1010D

http://codeforces.com/problemset/problem/1010/D

有n个节点,编号由1到n,1为根节点,按顺序给出1~n所具有的属性,按顺序输出,当输入端的值修改后根节点的值

按条件写代码,在查询之前因为多次查询需要提前打好表。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
class AP {
public:
    // XOR OR AND NOT IN
    int first, second;
    char name[4];
    bool value;
    void build () {
        scanf ( "%s", name );
        if ( name[0] == 'X' || name[0] == 'A' || name[0] == 'O' )
            scanf ( "%d%d", &first, &second );
        else if ( name[0] == 'N' ) scanf ( "%d", &first );
        else scanf ( "%d", &value );
    }
} in[1000004];
bool getvalue ( int m ) {
    bool f, s;
    if ( in[m].name[0] == 'I' ) return in[m].value;
    f = getvalue ( in[m].first );
    if ( in[m].name[0] != 'N' )
        s = getvalue ( in[m].second );
    if ( in[m].name[0] == 'X' ) in[m].value = f ^ s;
    else if ( in[m].name[0] == 'A' ) in[m].value = f & s;
    else if ( in[m].name[0] == 'O' ) in[m].value = f | s;
    else in[m].value = !f;
    return in[m].value;
}
bool ans[1000002];
void check ( int m, bool vju ) { //true 修改成功
    if ( in[m].name[0] == 'I' ) ans[m] = vju;
    else if ( in[m].name[0] == 'X' )
        ans[m] = vju, check ( in[m].first, vju ), check ( in[m].second, vju );
    else if ( in[m].name[0] == 'N' )
        ans[m] = vju, check ( in[m].first, vju );
    else if ( in[m].name[0] == 'A' ) {
        if ( !vju ) check ( in[m].first, vju ), check ( in[m].second, vju );
        else {
            if ( in[m].value ) check ( in[m].first, vju ), check ( in[m].second, vju );
            else if ( in[in[m].first].value != in[in[m].second].value )
                check ( in[m].first, !in[in[m].first].value ), check ( in[m].second, !in[in[m].second].value );
            else check ( in[m].first, false ), check ( in[m].second, false );
        }
    } else {
        if ( !vju ) check ( in[m].first, vju ), check ( in[m].second, vju );
        else {
            if ( !in[m].value ) check ( in[m].first, vju ), check ( in[m].second, vju );
            else if ( in[in[m].first].value != in[in[m].second].value )
                check ( in[m].first, in[in[m].first].value ), check ( in[m].second, in[in[m].second].value );
            else check ( in[m].first, false ), check ( in[m].second, false );
        }
    }
}
int main() {
    int n;
    while ( ~scanf ( "%d", &n ) ) {
        for ( int i = 1; i <= n; i++ )
            in[i].build ();
        getvalue ( 1 );
        memset ( ans, false, sizeof ans );
        check ( 1, true );
        for ( int i = 1; i <= n; i++ )
            if ( in[i].name[0] == 'I' )
                printf ( "%d", ans[i] ^in[1].value );
        printf ( "\n" );
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值