hdu_4944_FSF’s 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, width: 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)
InputThere 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).OutputOutput 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
HinIn the second test case, there are six levels(1x1,1x2,1x3,2x2,2x3,3x3)Here is the details for this game:
1x1: 1(K=1); 1x2: 2(K=1); 1x3: 3(K=1); 2x2: 2(K=1), 4(K=2); 2x3: 6(K=1); 3x3: 3(K=1), 9(K=3);
1+2+3+2+4+6+3+9=30
网上题解坑人,本来有点清楚了看了一会还把自己看懵了。重新理顺一下
根据题意写出式子,ans[n]=ans[n-1]+∑ n*i*k/gcd(n,i)
∑ n*i*k/gcd(n,i) //gcd(n,i)/k 为n的因子,
=n∗(1/a1+2/a2+⋯+n/an) //gcd(n,i)/k =ai,ai为n的因子 也就是说每个n的值对应了n的所有因子的贡献值之和。
比如n=2,因子1,2.
1的贡献 2 4
2的贡献 2
设mi为ai的因子 ∑ n*i*k/gcd(n,i)=n*[(1*m1/m1+2*m1/m1+...n/m1) +(1*m2/m2+2*m2/m2+...n/m2) +........+(1*mn/mn+2*mn/mn+...n/m)] //主要想清楚k/gcd()的值,k的变化对应mi的值
设sum(mi)=(1*mi/mi+2*mi/mi+...n/mi)=(n/m)*(n/m+1)/2
ans[n]=ans[n-1]+sum(mi)*mi
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
typedef long long LL;
#include<algorithm>
using namespace std;
#define N 500005
const LL mod=1LL<<32;
LL ans[N];
LL num[N];
int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout);
for(int i=1;i<N;i++)
{
for(LL j=i;j<N;j+=i)
{
num[j]+=(j/i)*(j/i+1)/2;
//cout<<j<<" "<<num[j]<<endl;
}
}
ans[1]=1;
for(LL i=2;i<=500000;i++)
{
ans[i]=ans[i-1]+num[i]*i%mod;
ans[i]%=mod;
//cout<<ans[i]<<endl;
}
int t;
scanf("%d",&t);
for(int l=1;l<=t;l++)
{
int n;
scanf("%d",&n);
cout<<"Case #"<<l<<": "<<ans[n]<<endl; }
}
hdu_4944_FSF’s game的更多相关文章
随机推荐
- Windows 10 JDK安装及环境配置(vim+gcc)
JDK安装 首先去官网下载JDK:点击进入 下载后点击安装: 中途会提示安装jre,注意jre的安装文件夹和jdk的不能相同,不然会覆盖掉jdk里面的jre文件.可以创建一个Java文件夹.将jdk和 ...
- .NET标准化题目
1. 下面对FxCop的描述中,错误的是:(D) A. FxCop是一个静态代码分析工具. B. 可以定制自己的规则加入FxCop引擎. C. FxCop主要是对.NET中托管代码的assembly进 ...
- http三次握手四次挥手
最近一直忙于看前端vue相关内容,后端相关内容没有跟进,文章停了3周,,,哎,还是懒吧!子曰生命在于运动,该学习还是要学的,文章嘛也还是要整理滴,不扯了--- 参考: https://blog.csd ...
- Linux禁ping
A.临时允许PING操作的命令为:# >/proc/sys/net/ipv4/icmp_echo_ignore_all B.永久允许PING配置方法. /etc/sysctl.conf 中增加一 ...
- PHP substr()函数
PHP substr()函数可以分割文字,但要分割的文字如果包括中文字符往往会遇到问题,这时可以用mb_substr()/mb_strcut这个函数,mb_substr() /mb_strcut的用法 ...
- Day6 下(
T1 模拟,80? #include<iostream> #include<cstring> #include<queue> #include<algorit ...
- ajax多次请求的一个效果思路
首先页面加载时候显示遮罩层 jQuery(function() { show_dialog(); //tianxie(); }); 定义一个全局数组,用于存放问题id var qar = []; 循环 ...
- 相同datatable合并
- Android打包混淆文件模板
# This is a configuration file for ProGuard. # http://proguard.sourceforge.net/index.html#manual/usa ...
- php的yii框架开发总结5
MVC架构之model类: 我的日报系统用到的数据表:tbl_dailyreport表 其中anthor_id是外键,对应tbl_user数据表的主键id,下面是tbl_user表 class Dai ...