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的更多相关文章
随机推荐
- Oracle安装后忘记用户名或密码+创建新登陆用户
新安装的Oracle11g,不料在使用的时候没记住安装时的用户名和密码. 不用担心,打开sqlplus. 按如下步骤,新建一个登陆用户: 第一步:以sys登陆 sys/密码 as sysdba 此 ...
- java 正则表达式提取价格
实例代码: public static void main(String[] args) { String str="11000.00元"; Pattern pattern = P ...
- B树和B+树对比,为什么MySQL数据库索引选择使用B+树?
一 基础知识 二叉树 根节点,第一层的节点 叶子节点,没有子节点的节点. 非叶子节点,有子节点的节点,根节点也是非叶子节点. B树 B树的节点为关键字和相应的数据(索引等) B+树 B+树是B树的一个 ...
- BZOJ4245: [ONTAK2015]OR-XOR(前缀和)
题意 题目链接 Sol 又是一道非常interesting的题目 很显然要按位考虑 因为最终答案是xor之后or,所以分开之后之后这样位上1的数量是一定是偶数,否则直接加到答案里面 同时,这里面有些部 ...
- log4j.dtd
<?xml version="1.0" encoding="UTF-8" ?> <!-- Licensed to the Apache Sof ...
- 移动webApp - 1像素实现(点5像素的秘密)
在移动web项目中,经常会实现以下1像素的边框 移动web设计中,在retina显示屏下网页会由1px会被渲染为2px,那么视觉稿中1px的线条还原成网页需要css定义为0.5px 但是正当我们去用0 ...
- 算法练习-字符串转换成整数(实现atoi函数)
练习问题来源 https://leetcode.com/problems/string-to-integer-atoi/ https://wizardforcel.gitbooks.io/the-ar ...
- selenium中Alter等弹出对话框的处理
昨天使用selenium做自动化测试,发现部分页面会弹出alert对话框,找了写资料,大概的意思就是要给弹出的对话框做出相应,不然,后续的处理会失败. _driver.SwitchTo().Alert ...
- [总结]SHAREPOINT - CAML列表查询(上)
首先要了解的是CAML(Collaboration Application Markup Language)不仅仅是用在对列表.文档库的查询,字段的定义,站点定义等处处使用的都是CAML. 简单的提一 ...
- Extjs4如何构造store基类
目标:重写一个BaseStore的基类,它继承自Ext.data.Store基类. autoLoad:true/false 是否自动加载,true时创建store即自动加载,一般适合get方式:fal ...