題目學習——韓信點兵

1077 韓信點兵

時間限制:500MS  內存限制:65536K

Description

 相傳漢高祖劉邦問大將軍韓信統御兵士多少,韓信答說,每3人一列餘1人、5人一列餘2人、7人一列餘4人、13人一列餘6人、 17人一列餘2人、19人一列餘10人、23人一列餘1人、29人一列餘11人。

劉邦茫然而不知其數。你呢? 你是一位優秀的程序員,請你幫劉邦解決這一問題。 


輸入格式

要求由鍵盤輸入A,B,C,D,E,F,G,H,a,b,c,d,e,f,g,h十六個數,分別代表每A人一列餘a、每B人一列餘b、每C人一列餘c、每D人一列餘D、每E人一列餘e、每F人一列餘f、每G人一列餘g、每H人一列餘h,其中A,B,C,D,E,F,G,H爲互不相等的質數


輸出格式

輸出總兵士數,要求輸出滿足條件的最小的一個,但要滿足8種排法的每一種排法至少可排一列。(保證給的數據,有結果且計算的結果不會超過2的63次方)


輸入樣例

2 3 5 7 11 13 17 19
1 1 1 1 1 1 1 1


輸出樣例

9699691


暑假看了點數論,在網上看了些中國剩餘定理,回頭寫這道數學題/呲牙


//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <string>
#include <stack>
#include <set>
#include <map>
#include <iostream>
#include <bitset>
#include <algorithm>
using namespace std;

#define MP make_pair
#define PB push_back
#define mst(a,b) memset((a),(b),sizeof(a))
#define TEST cout<<"*************************"<<endl

#define rep(s,n,up) for(int i = (s); i < (n); i+=(up))
#define per(n,e,down) for(int i = (n); i >= (e); i-=(down))
#define rep1(s,n,up) for(int j = (s); j < (n); j+=(up))
#define per1(n,e,down) for(int j = (n); j >= (e); j-=(down))

typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> Pii;
typedef vector<int> Vi;
typedef vector<Pii> Vii;
const int inf = 0x3f3f3f3f;
const LL INF = (1uLL << 63) - 1;
const double Pi = acos(-1.0);
const int maxn = (1 << 16) + 7;
const uLL Hashmod = 29050993;
const double esp=1e-6;


//#define local


int main() {
#ifdef local
    freopen("input.txt", "r", stdin);
    //freopen("output.txt","w",stdout);
#endif
    //ios::sync_with_stdio(0);
    //cin.tie();
    LL divisor[8],remainder[8],tot_m=1,sum=0;
    rep(0,8,1)scanf("%lld",&divisor[i]);
    rep(0,8,1)scanf("%lld",&remainder[i]);
    LL min_sum=divisor[0]+remainder[0];
    rep(0,8,1){
        tot_m*=divisor[i];
        LL s=divisor[i]+remainder[i];
        if(s>min_sum)
            min_sum=s;
    }
    rep(0,8,1){
        LL m=1,temp_m=1;
        rep1(0,8,1){
            if(j!=i)
                m*=divisor[j];
        }
        temp_m=m;
        while(m%divisor[i]!=1)
            m+=temp_m;
        sum+=m*remainder[i];
    }
    sum%=tot_m;
    while(sum<min_sum)sum+=tot_m;
    printf("%lld\n",sum);
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章