题目学习——韩信点兵

来源:互联网 发布:2017装修手机淘宝主页 编辑:程序博客网 时间:2024/06/06 23:26

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 191 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 localint 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;}



原创粉丝点击