5451 Best Solver

#题目

Best Solver
Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)
Total Submission(s): 1141    Accepted Submission(s): 655


Problem Description
The so-called best problem solver can easily solve this problem, with his/her childhood sweetheart.

It is known that y=(5+26√)1+2x.
For a given integer x (0≤x<232) and a given prime number M (M≤46337), print [y]%M. ([y] means the integer part of y)
 

Input
An integer T (1<T≤1000), indicating there are T test cases.
Following are T lines, each containing two integers x and M, as introduced above.
 

Output
The output contains exactly T lines.
Each line contains an integer representing [y]%M.
 

Sample Input
7
0 46337
1 46337
3 46337
1 46337
21 46337
321 46337
4321 46337
 

Sample Output
Case #1: 97
Case #2: 969
Case #3: 16537
Case #4: 969
Case #5: 40453
Case #6: 10211
Case #7: 17947

#分析

这显然是道快速幂的题目,不过我们首先需求出取整后的表达式,因为小数无法取模。

然后由于(5+sqrt(24))^n+(5-sqrt(24))^n一定为整数,且(5-sqrt(24))^n<1,得出(5+sqrt(24))^n的整数部分为

(5+sqrt(24))^n+(5-sqrt(24))^n-1,然后通过特征方程的方法将上面的式子转化为递推式(广义Fabonacci数列),又由于广义Fabonacci数列取模拥有循环节(周期性,详见博客https://blog.csdn.net/ACdreamers/article/details/25616461),求出循环节,再对1+2^x用快速幂进行分解,便可得出答案。

对于化为递推式:

类比于Fabonacci数列,F[n]=F[n-1]+F[n-2]形式,可以化为

所有的F[n]=p*F[n-1]+q*F[n-2]形式都能与A*x^n+B*y^n形式通过特征方程相互转换

(参考https://blog.csdn.net/happykocola/article/details/73933314https://blog.csdn.net/hanchengxi/article/details/17550429

对于这道题,F[n]=(5+sqrt(24))^n+(5-sqrt(24))^n,可以求出递推式F[n]=10*F[n-1]-F[n-2]。

#AC码

#include <iostream>
#include <string.h>
using namespace std;
int T;
int M;
long long x;
int f[47338];   //递推式取模后的值,也就是答案+1
//这里的数组大小详见推荐的博客,循环节的大小,这个值并非准确的值,理论上应该取M^2,不过M^2超内存

//寻找循环节长度,同时记录值
int f_cir()
{
    f[1]=10%M;
    f[2]=98%M;
    int i=3;
    while(1)
    {
        f[i]=(10*f[i-1]-f[i-2]+M)%M;   //加M是因为10*f[i-1]-f[i-2]可能为负
        if(f[i]==f[2]&&f[i-1]==f[1])
            return i-2;
        i++;
    }
}
int f_ans(int p)//p为周期,现在需求出(1+2^x)%p的值(等于2^x%p+1),对2^x%p使用快速幂
{
    int ans=1;
    int a=2;
    while(x)
    {
        if(x&1)
            ans=(ans*a)%p;
        a=(a*a)%p;
        x>>=1;
    }
    return (f[ans+1]-1)%M;  //答案为f[n]-1
}
int main()
{
    //freopen("5451.txt","r",stdin);
    cin>>T;
    for(int t=1;t<=T;t++)
    {
        cin>>x>>M;
        int per=f_cir();
        cout<<"Case #"<<t<<": ";
        cout<<f_ans(per)<<endl;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
优化这段代码 for j in n_components: estimator = PCA(n_components=j,random_state=42) pca_X_train = estimator.fit_transform(X_standard) pca_X_test = estimator.transform(X_standard_test) cvx = StratifiedKFold(n_splits=5, shuffle=True, random_state=42) cost = [-5, -3, -1, 1, 3, 5, 7, 9, 11, 13, 15] gam = [3, 1, -1, -3, -5, -7, -9, -11, -13, -15] parameters =[{'kernel': ['rbf'], 'C': [2x for x in cost],'gamma':[2x for x in gam]}] svc_grid_search=GridSearchCV(estimator=SVC(random_state=42), param_grid=parameters,cv=cvx,scoring=scoring,verbose=0) svc_grid_search.fit(pca_X_train, train_y) param_grid = {'penalty':['l1', 'l2'], "C":[0.00001,0.0001,0.001, 0.01, 0.1, 1, 10, 100, 1000], "solver":["newton-cg", "lbfgs","liblinear","sag","saga"] # "algorithm":['auto', 'ball_tree', 'kd_tree', 'brute'] } LR_grid = LogisticRegression(max_iter=1000, random_state=42) LR_grid_search = GridSearchCV(LR_grid, param_grid=param_grid, cv=cvx ,scoring=scoring,n_jobs=10,verbose=0) LR_grid_search.fit(pca_X_train, train_y) estimators = [ ('lr', LR_grid_search.best_estimator_), ('svc', svc_grid_search.best_estimator_), ] clf = StackingClassifier(estimators=estimators, final_estimator=LinearSVC(C=5, random_state=42),n_jobs=10,verbose=0) clf.fit(pca_X_train, train_y) estimators = [ ('lr', LR_grid_search.best_estimator_), ('svc', svc_grid_search.best_estimator_), ] param_grid = {'final_estimator':[LogisticRegression(C=0.00001),LogisticRegression(C=0.0001), LogisticRegression(C=0.001),LogisticRegression(C=0.01), LogisticRegression(C=0.1),LogisticRegression(C=1), LogisticRegression(C=10),LogisticRegression(C=100), LogisticRegression(C=1000)]} Stacking_grid =StackingClassifier(estimators=estimators,) Stacking_grid_search = GridSearchCV(Stacking_grid, param_grid=param_grid, cv=cvx, scoring=scoring,n_jobs=10,verbose=0) Stacking_grid_search.fit(pca_X_train, train_y) var = Stacking_grid_search.best_estimator_ train_pre_y = cross_val_predict(Stacking_grid_search.best_estimator_, pca_X_train,train_y, cv=cvx) train_res1=get_measures_gridloo(train_y,train_pre_y) test_pre_y = Stacking_grid_search.predict(pca_X_test) test_res1=get_measures_gridloo(test_y,test_pre_y) best_pca_train_aucs.append(train_res1.loc[:,"AUC"]) best_pca_test_aucs.append(test_res1.loc[:,"AUC"]) best_pca_train_scores.append(train_res1) best_pca_test_scores.append(test_res1) train_aucs.append(np.max(best_pca_train_aucs)) test_aucs.append(best_pca_test_aucs[np.argmax(best_pca_train_aucs)].item()) train_scores.append(best_pca_train_scores[np.argmax(best_pca_train_aucs)]) test_scores.append(best_pca_test_scores[np.argmax(best_pca_train_aucs)]) pca_comp.append(n_components[np.argmax(best_pca_train_aucs)]) print("n_components:") print(n_components[np.argmax(best_pca_train_aucs)])
07-14
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值