第五届华中区程序设计邀请赛暨武汉大学第十四届校赛 现场赛 WHU oj Problem 1613 - Count in Sama’s triangle 扩展欧几里得




链接:戳这里


Time Limit: 1000MS   Memory Limit: 65536KB   
Description
        Today, the math teacher taught Alice Hui Yang’s triangle. However, the teacher came up with a new one, called Sama’s triangle (some of you may be familiar with this name).

       In this triangle, there is only 30 row, and the ith row has i-1 number, f[i][1] , f[i][2], …, f[i][i-1] (1strow has no number). And initially, f[i][0] = a, f[i][i] = b, (0<a, b) then f[i][j] = f[i-1][j-1] + f[i-1][j] (for all i from 1 to 30 , for all j in 1 to i-1, inclusive).
       2ndrow    f[2][1]
       3rdrow     f[3][1]     f[3][2]
       4throw     f[4][1]     f[4][2]     f[4][3]
       30throw   f[30][1]   f[30][2]   f[30][3]   …    f[30][29]
       Now the teacher asked, ‘how many M are in the triangles among all pairs of (a, b).
Input
The input consists of several test cases.
The first line consists of one integer T (T <= 500), meaning the number of test cases.
For each test cases, there is one integer M (M<= 1e9), which is asked from the teacher.
Here, 1eX means 1000…0000 (x 0s).
Output
For each test case, output the times number M shows up in all positions of all triangles.
Sample Input
1
3
Sample Output
4


题意:

给出30*30的矩阵 其中j>=i的矩阵部分不要

 f[i][0] = a, f[i][i] = b, (0<a, b) 并且 f[i][j] = f[i-1][j-1] + f[i-1][j]

给出一个m问  对于任意的(a,b)能产生多少个m  这里的意思是多少个位置存在m 对于所有的(a,b)组合


思路:

推出三角矩阵发现所有的位置上都是关于一个x*a+y*b (x,y为常数)的一元二次方程  

然后我们需要求出这个位置上的x*a+y*b==m 有多少解  也就是有多少对这样的(a,b)组合

并且每个位置的a,b互不影响  (后来证明了其实也影响不了  因为所有的(x,y)都是不想同的

下面就是求不定方程a*x+b*y==m有多少正整数解  完美利用扩展欧几里得求出贡献即可

这里还需要注意的就是a*x+b*y=m要存在正整数解的话 需要满足 m%gcd(a,b)==0 


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
ll m;
struct node{
    ll x,y,g;
    node (ll x=0,ll y=0):x(x),y(y){}
}dp[55][55];
void exgcd(ll a,ll b,ll &d,ll &x,ll &y){
    if(!b){
        d=a;x=1;y=0;
    } else {
        exgcd(b,a%b,d,y,x);
        y-=(a/b)*x;
    }
}
ll gcd(ll a,ll b){
    return b==0 ? a : gcd(b,a%b);
}
int main(){
    for(int i=0;i<=30;i++) dp[i][0].x=1;
    for(int i=0;i<=30;i++) dp[i][i].y=1;
    for(int i=2;i<=30;i++){
        for(int j=1;j<i;j++){
            dp[i][j].x=dp[i-1][j-1].x+dp[i-1][j].x;
            dp[i][j].y=dp[i-1][j-1].y+dp[i-1][j].y;
            dp[i][j].g=gcd(dp[i][j].x,dp[i][j].y);
        }
    }
    /*for(int i=2;i<=30;i++){
        for(int j=1;j<i;j++){
            cout<<dp[i][j].x<<"+"<<dp[i][j].y<<" ";
        }
        cout<<endl;
    }*/
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%lld",&m);
        ll ans=0;
        for(int i=2;i<=30;i++){
            for(int j=1;j<i;j++){
                if(m%dp[i][j].g==0){
                    ll a=dp[i][j].x/dp[i][j].g,b=dp[i][j].y/dp[i][j].g,d,x,y;
                    ll M=m/dp[i][j].g;
                    exgcd(a,b,d,x,y);
                    x*=M;y*=M;
                    if(x<=0){
                        ll num=1LL-x/b;
                        x+=num*b;
                        y-=num*a;
                    }
                    if(x>b){
                        ll num=(x-1)/b;
                        x-=num*b;
                        y+=num*a;
                    }
                    if(y<=0) continue;
                    ans+=(y+a-1LL)/a;
                }
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值