zoukankan      html  css  js  c++  java
  • hdu 4944

    FSF’s game

    Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
    Total Submission(s): 1135    Accepted Submission(s): 582


    Problem Description
    FSF has programmed a game.
    In this game, players need to divide a rectangle into several same squares.
    The length and width of rectangles are integer, and of course the side length of squares are integer.

    After division, players can get some coins.
    If players successfully divide a AxB rectangle(length: A, B) into KxK squares(side length: K), they can get A*B/ gcd(A/K,B/K) gold coins.
    In a level, you can’t get coins twice with same method.
    (For example, You can get 6 coins from 2x2(A=2,B=2) rectangle. When K=1, A*B/gcd(A/K,B/K)=2; When K=2, A*B/gcd(A/K,B/K)=4; 2+4=6; )

    There are N*(N+1)/2 levels in this game, and every level is an unique rectangle. (1x1 , 2x1, 2x2, 3x1, ..., Nx(N-1), NxN)

    FSF has played this game for a long time, and he finally gets all the coins in the game.
    Unfortunately ,he uses an UNSIGNED 32-BIT INTEGER variable to count the number of coins.
    This variable may overflow.
    We want to know what the variable will be.
    (In other words, the number of coins mod 2^32)
     
    Input
    There are multiply test cases.

    The first line contains an integer T(T<=500000), the number of test cases

    Each of the next T lines contain an integer N(N<=500000).
     
    Output
    Output a single line for each test case.

    For each test case, you should output "Case #C: ". first, where C indicates the case number and counts from 1.

    Then output the answer, the value of that UNSIGNED 32-BIT INTEGER variable.
     
    Sample Input
    3
    1 3 100
     
    Sample Output
    Case #1: 1
    Case #2: 30
    Case #3: 15662489
     
     
    析:指定n阶矩形共n*(n+1)/2个,为边长A*B为(1*1,1*2,2*2,2*3,...,(n-1)*n,n*n),然后计算将这n阶矩形分解成k*k的正方形代价为A*B/(gcd(A/k,B/k)),要求计算总代价
      可令代价和为res[n] ,则知res[n-1]包括(1*1,1*2,2*2,2*3,...,(n-2)*(n-1),(n-1)*(n-1)),而res[n] = res[n-1] + 代价(1*n,2*n,3*n,...,(n-1)*n,n*n),
      故可令tmp[n] = 代价(1*n,2*n,3*n,...,(n-1)*n,n*n),所以求总代价,先预处理所有tmp即可。而对每一个矩形i*n的代价 = i*n/gcd(i/k,n/k) = n*(i/j1+i/j2+i/j3...) (其中gx为n与i的公因数)
      所以可以枚举所有i与n的公因子,对每一个公因子j,存在于n以内的数有j,2*j,3*j,...,n/j,故代价i*n = Σn*(1+2+...+n/j) = Σn*(1+n/j)*(n/j)/2
     
    #include <math.h>
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #define ll long long
    #define mod 4294967296
    using namespace std;
    const int N = 500005;
    int t, n, m, ans;
    ll res[N], tmp[N];
    void init(){
        for(ll i = 1; i < N; i ++){
            for(ll k = i; k < N; k += i){
                tmp[k] += (1+k/i)*(k/i)/2;
                tmp[k] %= mod;
            }
        }
        for(int i = 1; i < N; i ++){
            res[i] = (res[i-1]+tmp[i]*i)%mod;
        }
    }
    int main(){
        init();
        scanf("%d", &t);
        for(int i = 1; i <= t; i ++) {
            scanf("%d", &n);
            printf("Case #%d: %lld
    ", i, res[n]);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    Create procedure
    json
    XSLT
    使用 ActiveMQ 示例
    使用Apache FtpServer搭建FTP服务器
    Publisher/Subscriber(发布/订阅者)消息模式开发流程
    使用 ActiveMQ 示例
    内嵌jetty
    基于Atom协议的数据接入规范
    C++创建jni 并且利用rundll32.exe调试jni程序
  • 原文地址:https://www.cnblogs.com/microcodes/p/12814234.html
Copyright © 2011-2022 走看看