Sicily 1841. Atom Transit

1841. Atom Transit

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

     Physician Thomson has discovered a new kind of atoms. The atoms have infinite energy levels, which can be represented by integers: -∞,...,–1,0,1,2,...,+∞.Each atom can transit from one energy level to another in four ways, +a1,+a2,+a3,+a4. For example, suppose a1=1,a2=2,a3=-3,a4=-4, an atom has initial energy level 0, if it transits in the first way, then its energy level will become 1, and if it transits in the last way, then its energy level will be –4. 
    In a special environment, two atoms always transit simultaneously, and in each transit their energy levels change independently. If after several times(may be 0) of transit, the two atoms have the same energy levels, they will collide and explode, then the pair of initial energy levels are called “unstable”, otherwise They are called “stable”. For example, if the initial energy levels of the two atoms are 2 and 1, the four transit increments are 0,1,2 and 3, obviously the pair of initial energy levels are “unstable”, because if the first atom transits in the first way, the second atom transits in the second way, they will have the same energy levels. 
    Given the range of each atom’s initial energy levels, your task is to determine how many pairs of initial energy levels are “unstable”.

Input

     Input may have several test cases. The first line is an integer, n, the number of cases followed. For each case, there is one line containing six integers,
min,max,a1,a2,a3,a4,(-50000<=min<=max<=50000,-1000000<=a1,a2,a3,a4<=1000000) 
the first two numbers specified that the initial energy level of each atom is in the range [min,max], and the other four numbers specified the four ways of transit.

Output

     For each test case, output an integer in one line, specifying the number of “unstable” pairs. No extra spaces are allowed.

Sample Input

2
0 2 1 2 1 2
1 10 1 2 3 4

Sample Output

9

100

// Problem#: 1841
// Submission#: 3590278
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>

int cs, min, max, a1, a2, a3, a4;

int gcd(int x, int y) {
    int temp;
    while (y) {
        temp = x;
        x = y;
        y = temp % y;
    }
    return x;
}

int main() {
    int i, g;
    scanf("%d", &cs);
    while (cs--) {
        scanf("%d%d%d%d%d%d", &min, &max, &a1, &a2, &a3, &a4);
        a1 -= a4;
        a2 -= a4;
        a3 -= a4;
        g = gcd(a1, gcd(a2, a3));
        if (!g) printf("%d\n", max - min + 1);
        else {
            if (g < 0) g = -g;
            long long n = max - min;
            long long ans = (n / g) * ((n / g - 1) * g + (n % g + 1) * 2) + n + 1;
            printf("%lld\n", ans);
        }
    }
    return 0;
}                                 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值