第五届华中区程序设计邀请赛暨武汉大学第十四届校赛 现场赛 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        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

给出一个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
#include
#include
#include
#include
#include
#include 
#include
#include
#include
#include
#include
#include
#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;jb){
                        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;
}


你可能感兴趣的:(扩展欧几里德)